A Better Way to Write Multiple OR Conditions?


#1

Hi, Code People!

Is there a better way to write multiple Or conditions:

(val2===4||val2===9||val2===16||val2===25||val2===36||val2===49||val2===64||val2===81||val2===100)

Just as an FYI: I have it as part of an If statement – and Switch statement, too…FWIW.

Thanks!!


#2

In that particular case you’d probably instead want to do a bit of math to see if it’s a square
Otherwise stick it in an array and use a loop, perhaps you’d make a function which tests if a is in b, or perhaps something like that already exists for you to use (Array.some)

If it seems repetitive to write some code then you should probably write code that carries out that repetition instead


#3

Indeed – but when the repetition is vastly simpler than its abstraction would be (that is, simpler to just type out a relatively few cases/results than conjure up some arithmetic/algebra [which, actually, I couldn’t figure out for like half an hour since I myself do the simple thing IRL and just keep multiplying to find square roots]), I was like…hmm, maybe there’s some kinda syntax that I’m not aware of…

So I guess there isn’t any in this situation. Thanks for the confirmation!


#4

Most programs are useful because we can encode our reasoning into them and carry it out faster and/or more times.

Another data structure that is able to test if it contains something is a hash set, for large sizes it is far more efficient than comparing to each value. You could create a set of your numbers, and then test if X is contained in that set
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Set
It’s different in how it behaves, but you would write the same thing with an array
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/includes

Considering that you do have to list each number, there’s not much potential to remove anything more


#5

Oh yes, and believe it or not I’ve had a Dickens of a time last night (technically this “morning!”) finally coming up with

((val2 === parseInt(Math.sqrt(val2)) * parseInt(Math.sqrt(val2))) && (val2 !== 0 && val2 !== 1))

for a slightly more “elegant” replacement – simple as that is!

Thanks for the tip – a bit beyond me now but I will certainly keep it in mind going forward!!


#6

Mmmh it might just do what you want but it makes me nervous.
Parsing means reading text, but you’re dealing with numbers. It may end up with the same result because of implicit conversion to string but it means something else. Math.round or Math.ceil would be more appropriate.

Rather than computing the square root twice, use a variable to store the result so that you can use it more than once.

Seeing floating point math mixed with === operators also makes me unhappy, because floats are approximations. Ideally this should only use integer operations to avoid cases like 3.9999999999999997 getting truncated to 3, turning a very small error (which would have been avoided sticking to integers) into a very large one.
(Trying to get an exact answer while using inexact math operations means having to reason a whole lot about whether it’s correct or not, it’s much easier to stick with integers, floats are often not what you want)
…On the other hand js doesn’t provide an integer version of sqrt, so the reasonable thing here is to use Math.sqrt anyway, and use Math.round which will correct any errors because they’ll be very small (unless input numbers are very large) and will therefore get rounded in the right direction)
So, same thing but use Math.round instead which is more appropriate.
Or do what you did manually. Count up. It’s really naive but for small numbers it won’t matter, what does matter is that it is obviously correct. One can also double/halve to narrow in on the right number, this would be efficient for all practical inputs (up to the point where javascript can’t accurately represent the number anyway)

bottom line, floats are usually not what you want. use integers.


#7

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.