+ 1
[SOLVED] ternary statement returns undefined?
https://code.sololearn.com/Wp5R5gL7Gd2x PS: my code is working slightly fine, the only problem? There's an extra alert with message undefined. why do this alert undefined if I don't enter (null/"") any value or wrong value. You will see the alert with message undefined, after making a correct or !null input. I don't know if the problem falls here: (userSelection !== "rock" || userSelection !== "paper" || userSelection !== "scissor")? userSelectionWrong(): (userSelection === "")? userSelectionWrong() : "Something Happen, Error"; alert(result); please check the code, to see the problem, many thanks
13 Antworten
+ 2
This will always be true:
(userSelection !== "rock" || userSelection !== "paper" || userSelection !== "scissor")
Rethink the logic of this part. Do you fully understand "||" (OR) operator?
+ 2
Also, pls check your indentation. You seemed to indent all the JS code one level in excess.
Good indentation helps reading.
+ 2
Emerson Prado I seriously felt sorry, your just helping, I hope that if you felt bad from me denying the truth about || operator, my word "sorry" can make up for that. You guys just teach me || operator. Yeah I don't know OR, that's great I gained correction. Hope you guys understand that I'm just fighting for what I know is "right" for me, which revealed totally wrong. I guess I now understand the reason of appearing prompt with undefined value, it's because I'm looping at functions, not using for loop.
+ 2
Jonathan P. Jundarino No problem at all. Sometimes we have to fight our certainties to learn something new. And it takes a couple provoking rounds sometimes.
Is the code 100% OK now?
+ 2
Emerson Prado that's good to know, I guess that's 100% ok now. Thanks so much guys.
+ 1
Jayakrishna🇮🇳 it's working fine, that error will appear if you cancel the prompt.
+ 1
Jonathan P. Jundarino OK. If it is correct to always return true, why use the conditional?
I refer to the indentation of the whole code. Why isn't there any line at level zero?
+ 1
Jonathan P. Jundarino Is that not your problem?
alert( result ) => undefined when wrong input?
Using || will result always true, but yes works as you expecting it will reach there when only input is not valid so || or && both works same but actually no need to check.. It's already last else part.
On valid inputs, it will not get checked actually..
If you use it on first check then you must use && , not ||.
I thought, your trouble is with alert..!
Ok. Is it solved...?
+ 1
Jayakrishna🇮🇳 thanks for also saying that. && Operator fixed the endless loop.
0
Another question, 7 views already, but no one answered. Is my code that hard to read? If yes, then any comment on how to improve it?
0
userSelectionWrong() does not returning anything. .
Comment line 63:
It's redentend also..
Hope it helps...
0
Emerson Prado
"Rethink the logic of this part. Do you fully understand "||" (OR) operator?"
Reply: that's working fine, it means:
if userSelection is not "rock" or not "paper" or not "scissor"
My indentation is also good, that's just multiple ternary statement.
0
Emerson Prado yeah, Thanks, I know now what you mean, indentation. There's no line at level zero. But the || (OR) operator is not always true. That would be false if input is "rock" "paper" and "scissor"