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

JavaScript

Re-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

Hi 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...

Thank you very much.

Could you possibly explain what the 'ask' does?

ask_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.

@Vaughan - You should review how functions work in JavaScript