Welcome to the Treehouse Community
Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.
Looking to learn something new?
Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.
Start your free trial
Vaughan Slater
Courses Plus Student 3,337 PointsRe-do a function if condition is met
Hi Guys,
I've just gone through the basic programming parts and I just wanted to make a simple little text game. For fun, get's me used to writing a few things.
In the below code, if the 'else' statement is triggered I want them to have to re-do the prompt.
var concent = prompt("Would you like to go on an adventure?");
if(concent == "yes"){
alert("That's great news! Let's get started.");
} else if(concent == "no"){
alert("Oh that's a shame, I guess I'll have to look for someone else..");
} else {
alert("I dont know what you mean by that answer. Try again!");
}
How would I go about doing this?
4 Answers
Hardik Dangar
Courses Plus Student 856 PointsHi Vaughan,
I think you are looking to repeat the prompt and you could use recursion via procedure like .....
<script type="text/javascript">
function ask_prompt(){
var concent = prompt("Would you like to go on an adventure?");
if(concent == "yes"){
alert("That's great news! Let's get started.");
} else if(concent == "no"){
alert("Oh that's a shame, I guess I'll have to look for someone else..");
} else {
alert("I dont know what you mean by that answer. Try again!");
ask_prompt();
}
}
ask_prompt();
</script>
Hope this helps...
Vaughan Slater
Courses Plus Student 3,337 PointsThank you very much.
Could you possibly explain what the 'ask' does?
Hardik Dangar
Courses Plus Student 856 Pointsask_prompt is just a function name. and i just called it at the last so when browser loads file it will execute it first time.
James Barnett
39,199 Points@Vaughan - You should review how functions work in JavaScript