[SOLVED] ternary statement returns undefined? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 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

24th Jun 2022, 6:46 AM
Jonathan P. Jundarino
Jonathan P. Jundarino - avatar
13 Answers
+ 2
This will always be true: (userSelection !== "rock" || userSelection !== "paper" || userSelection !== "scissor") Rethink the logic of this part. Do you fully understand "||" (OR) operator?
24th Jun 2022, 10:15 AM
Emerson Prado
Emerson Prado - avatar
+ 2
Also, pls check your indentation. You seemed to indent all the JS code one level in excess. Good indentation helps reading.
24th Jun 2022, 10:20 AM
Emerson Prado
Emerson Prado - avatar
+ 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.
24th Jun 2022, 12:02 PM
Jonathan P. Jundarino
Jonathan P. Jundarino - avatar
+ 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?
24th Jun 2022, 10:07 PM
Emerson Prado
Emerson Prado - avatar
+ 2
Emerson Prado that's good to know, I guess that's 100% ok now. Thanks so much guys.
24th Jun 2022, 10:57 PM
Jonathan P. Jundarino
Jonathan P. Jundarino - avatar
+ 1
Jayakrishna🇮🇳 it's working fine, that error will appear if you cancel the prompt.
24th Jun 2022, 10:54 AM
Jonathan P. Jundarino
Jonathan P. Jundarino - avatar
+ 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?
24th Jun 2022, 11:02 AM
Emerson Prado
Emerson Prado - avatar
+ 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...?
24th Jun 2022, 11:22 AM
Jayakrishna 🇮🇳
+ 1
Jayakrishna🇮🇳 thanks for also saying that. && Operator fixed the endless loop.
24th Jun 2022, 11:51 AM
Jonathan P. Jundarino
Jonathan P. Jundarino - avatar
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?
24th Jun 2022, 8:35 AM
Jonathan P. Jundarino
Jonathan P. Jundarino - avatar
0
userSelectionWrong() does not returning anything. . Comment line 63: It's redentend also.. Hope it helps...
24th Jun 2022, 9:59 AM
Jayakrishna 🇮🇳
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.
24th Jun 2022, 10:46 AM
Jonathan P. Jundarino
Jonathan P. Jundarino - avatar
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"
24th Jun 2022, 11:08 AM
Jonathan P. Jundarino
Jonathan P. Jundarino - avatar