I need help on an if / else if script! | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 3

I need help on an if / else if script!

So I have been attempting to create a javascript recently that will respond to specific responses, and for some reason this script will not function: var name = prompt("What is your name?"); document.write("Hello " + name + ", are you feeling good or bad today?"); var response = prompt("How are you?"); if (response == "good") { document.write("You're doing " + response + "? Thats great to know! Goodbye!"); } if else (response == "bad") { var scale = prompt("Aw, thats too bad! On a scale of 1 through 10, how bad do you think youre feeling?"); if (scale == "1") { document.write("Only 1? That shouldnt be that bad!") } else if (scale == "2") { document.write("Hey, 2 isnt the worst! Cheer up!"); } else if (scale == "3") { document.write("Hey, at least it isnt 4!"); } else if (scale == "4") { document.write("4? Cheer up!"); } else if (scale == "5") { document.write("5? Meh."); } else if (scale == "6") { document.write("Well, at least it isnt 7!"); } else if (scale == "7") { document.write("Hey! 7 is my favorite number!"); } else if (scale == "8") { document.write("8 is less than 9, right? Cheer up!"); } else if (scale == "9") { document.write("Hey, at least 9 isnt a double digit!"); } else if (scale == "10") { document.write("Well arent you negative! Go find something to cheer yourself up!"); } else { document.write("Hey, I said 1-10! My processor isnt intelligent enough for those types of numbers!"); } Any help is appreciated!

30th Mar 2017, 11:29 PM
iBrandon
iBrandon - avatar
5 Answers
+ 1
I think you got your condition wrong at 2nd condition statement cause as far I know condition starts with if statement, ends with else and in the middle multiple statement are carried out as else if, you can also use as much nested statement accordingly.
30th Mar 2017, 11:53 PM
John Watson
John Watson - avatar
+ 1
Thank you guys so much for the advice! I have finally properly finished my script! Here it is if you would like to try it out! var name = prompt("What is your name?"); var response = prompt("Are you feeling good or bad today?"); if (response == "good") { document.write("You're doing well " + name + ", Thats great to know! Goodbye!"); } else if (response == "bad") { document.write("I see, " + name + ". "); var scale = prompt("Aw, that's too bad! On a scale of 1 through 10, how bad do you think youre feeling?"); if (scale == "1") { document.write(" Only 1? That shouldn't be that bad!") } else if (scale == "2") { document.write(" Hey, 2 isn't the worst! Cheer up!"); } else if (scale == "3") { document.write(" Hey, at least it isn't 4!"); } else if (scale == "4") { document.write(" 4? Cheer up!"); } else if (scale == "5") { document.write(" 5? Meh."); } else if (scale == "6") { document.write(" Well, at least it isn't 7!"); } else if (scale == "7") { document.write(" Hey! 7 is my favorite number!"); } else if (scale == "8") { document.write(" 8 is less than 9, right? Cheer up!"); } else if (scale == "9") { document.write(" Hey, at least 9 isn't a double digit!"); } else if (scale == "10") { document.write(" Well arent you negative! Go find something to cheer yourself up!"); } else { document.write(" Hey, I said 1-10! My processor isn't intelligent enough for those types of numbers!"); } }
31st Mar 2017, 12:37 AM
iBrandon
iBrandon - avatar
+ 1
Err... I don't believe you can copy comments... Oh well 😂
31st Mar 2017, 12:38 AM
iBrandon
iBrandon - avatar
0
you have a if else after the first if instead of else if. You're also missing a } bracket at the end of the code, you've closed the else statement but not the else if (response == "bad") statement. :)
30th Mar 2017, 11:49 PM
Hassie
Hassie - avatar
0
First problem I see is the first 'else if' is incorrectly written as 'if else (respense == "bad")'. i.e. change "if else" to "else if".
30th Mar 2017, 11:50 PM
Jehad Al-Ansari
Jehad Al-Ansari - avatar