+ 3

Repeating the If Statement?

Okay, so I don't know how to really word this, so I'm gonna provide a snippet of code, and possibly if you need me to explain more than I do, then I will without issue. Say you have a code that's like {I'm gonna use JS because That's the language I am trying to use it in} say we ask for a users age, if(age < 13) { window.alert("Younger than 13") } else if (13 < age) { window.alert("Older than 13") } else if (13 == 13) { window.alert("You're 13") } else { //instead of doing the same thing over and over again, //in this line, is there a way to just redo the check? }

9th Apr 2017, 4:13 PM
Dillion Piazza
Dillion  Piazza - avatar
32 Respostas
+ 2
Ok, I created a finalized version of your snippet of code: var age = prompt("How old are you?"); if (age < 13) { alert("Younger than 13"); } else if (age > 13) { alert("Older than 13"); } else if (age == 13) { alert("Is 13"); } else { statement() } function statement() { var age = prompt("I'm sorry, I didn't understand that statement. Please restate your answer. How old are you?"); if (age < 13) { alert("Younger than 13"); } else if (age > 13) { alert("Older than 13"); } else if (age == 13) { alert("Is 13"); } else { statement() } } As you can see, if the user types in anything but a number, it will call in the function "statement" and then repeat the age prompt. It will force the person to respond with an actual number in order to advance within the page. Im attempting to make it more compact right now by adding more functions and less commands, but for now this should work.
9th Apr 2017, 6:29 PM
iBrandon
iBrandon - avatar
+ 3
@Eranga Like instead of in the else statement, instead of doing : if(age < 13) { window.alert("Younger than 13") } else if (13 < age) { window.alert("Older than 13") } else if (13 == 13) { window.alert("You're 13") } else { //instead of doing the same thing over and over again, //in this line, is there a way to just redo the check? } Over and over again, I was wondering if there's just a way to avoid that to save lines of code
9th Apr 2017, 4:23 PM
Dillion Piazza
Dillion  Piazza - avatar
+ 3
@iBrandon I am talking in general, but in this case, I'm trying to get it so it would be like What if they enter a word like "aguih" instead of a age?
9th Apr 2017, 4:53 PM
Dillion Piazza
Dillion  Piazza - avatar
+ 3
@iBrandon please do, I apologize if I am asking for too much :)
9th Apr 2017, 4:56 PM
Dillion Piazza
Dillion  Piazza - avatar
+ 3
@iBrandon so make it in a function?
9th Apr 2017, 5:00 PM
Dillion Piazza
Dillion  Piazza - avatar
+ 3
@Eranga is that JS only thing though ?
9th Apr 2017, 5:01 PM
Dillion Piazza
Dillion  Piazza - avatar
+ 3
@iBrandon so the If Statement would be in a function ? Sorry, I have a mild form of autism
9th Apr 2017, 5:02 PM
Dillion Piazza
Dillion  Piazza - avatar
+ 3
@iBrandon if thats what I would want, that would mean put the if statements in a function ?
9th Apr 2017, 5:15 PM
Dillion Piazza
Dillion  Piazza - avatar
+ 3
Thank you (:
9th Apr 2017, 5:17 PM
Dillion Piazza
Dillion  Piazza - avatar
+ 3
That works, thanks :D
10th Apr 2017, 2:22 AM
Dillion Piazza
Dillion  Piazza - avatar
+ 2
@Eranga I don't fully understand what that is, I just watched a video on it
9th Apr 2017, 4:40 PM
Dillion Piazza
Dillion  Piazza - avatar
+ 2
@Eranga I may have multiple "ifs" and variables, so I don't know :/
9th Apr 2017, 4:51 PM
Dillion Piazza
Dillion  Piazza - avatar
+ 2
@iBrandon but would it loop the asking?
9th Apr 2017, 4:52 PM
Dillion Piazza
Dillion  Piazza - avatar
+ 2
@iBrandon but the thing about that, is right after that, it's just gonna end the program
9th Apr 2017, 4:57 PM
Dillion Piazza
Dillion  Piazza - avatar
+ 1
So, like @Eranga said, you could do a while statement to re-check the statement until another instance occurs.
9th Apr 2017, 4:50 PM
iBrandon
iBrandon - avatar
+ 1
What script are you attempting to create?
9th Apr 2017, 4:52 PM
iBrandon
iBrandon - avatar
+ 1
Ohh! I actually created a script on a topic sort of like this earlier! I'll show you an example of one of my previous scripts:
9th Apr 2017, 4:56 PM
iBrandon
iBrandon - avatar
+ 1
This is similar to what you are attempting to make: var name = prompt("What is your name?"); var response = prompt("Nice to meet you, " + name + ". Are you feeling good or bad today?"); if (response == "good") { document.write("You're doing well " + name + ", that's 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!"); } } else { document.write("You're doing " + response + "? I'm sorry, but I don't understand that statement."); }
9th Apr 2017, 4:56 PM
iBrandon
iBrandon - avatar
+ 1
As you can see, I added an else statement at the end so that if their answer is not "good" or "bad", it will give them a different response.
9th Apr 2017, 4:58 PM
iBrandon
iBrandon - avatar
+ 1
Well, you would have to add more script to the end such as a function if you wanted it to continue. For mine it just gives a response and then ends the script.
9th Apr 2017, 4:59 PM
iBrandon
iBrandon - avatar