Sololearn: Learn to Code
New course! Every coder should learn Generative AI!
Try a free lesson
+ 2
No, it has to be written like this if (x == "one" || x == "two") { document.write("hello") } Otherwise, it will always print hello no matter what because the string "two" is a truthy value.
23rd Dec 2019, 9:21 AM
ReimarPB
ReimarPB - avatar
+ 1
The syntax is valid, but the logic is not. As-is the block of code will execute no matter what x is equal to. Even if you change the strings to integers the logic is still the same. As-is the conditional statement reads [if x is equal to "one" is true or "two" is true, then print hello to the document], a non-empty string is always true. In order to get the code to perform as expected, you need the know if x is equal to "one" or is x is equal to "two". https://code.sololearn.com/W21qURYF60Ch/#js
23rd Dec 2019, 9:32 AM
ODLNT
ODLNT - avatar
0
if you want x to be equal to the string values 'one' or 'two', then yes. However, if you want x to be equal to numeric values 1 or 2, then replace 'one' and 'two' with 1 and 2.
23rd Dec 2019, 7:55 AM
Dr.Hippo
Dr.Hippo - avatar