Sunday, June 2, 2024
158
rated 0 times [  165] [ 7]  / answers: 1 / hits: 94376  / 11 Years ago, mon, february 3, 2014, 12:00:00

I'm new to programming in JavaScript, and I need help!
I am making a small program that randomly selects a number (1-4) and then prints out a statement based on the number. A=1, 2=B, 3=C, and 4=D. My problem is that with my if/else statements, I want the else statement to do nothing. How? (Sorry if this is a dumb question, I'm new to JS) The code is:



 var randomGuess=Math.floor(Math.random()*4)+1);
if (randomGuess===1) {
document.write(Try A.); }
else {
return false }
if (randomGuess===2) {
document.write(Try B.) }
else {
return false }
if (randomGuess===3) {
document.write(Try C.) }
else {
return false }
if (randomGuess===4) {
document.write(Try D.) }
else {
return false }

More From » if-statement

 Answers
82

an if doesn't need an else always .. so remove it.


if(){ ...}
if(){ ...}
if(){ ...}

is fine , but its adviced to use switch in this case.


switch works better in this kind of situation


[#72763] Sunday, February 2, 2014, 11 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
luna

Total Points: 698
Total Questions: 114
Total Answers: 93

Location: Israel
Member since Wed, Apr 14, 2021
3 Years ago
luna questions
;