How Do I Restart A Function From Within Itself? - Codecademy

Skip to ContentProfile image of netherlands2Submitted by netherlands2over 10 yearsHow do I Restart a Function From Within Itself?

In the section where I begin declaring/initializing the ‘compare’ function - I am trying to get the function to ‘restart’ if the user enters something that is not rock, paper or scissors. But instead - it continues to loop back to and execute the ‘newChoice’ variable. At first I thought it was b/c I’m trying to re-call the function w/in itself. But I’ve looked around a little - at recursion? - and at least have seen that you can call functions (or methods?) w/in themselves. What should I do to make it re-execute the function entirely - rather than merely re-executing the ‘newChoice’ command?

– I haven’t moved past the rock, paper, scissors lesson yet - so if there’s something more I need/will likely learn that’ll help just tell me and I’ll move onto them and then come back and try and figure this out again.–

var userChoice = prompt("Do you choose rock, paper or scissors?"); var computerChoice = Math.random(); if (computerChoice < 0.34) { computerChoice = "rock"; } else if(computerChoice <= 0.66) { computerChoice = "paper"; } else { computerChoice = "scissors"; } console.log("Computer: " + computerChoice); //-------------TRYING TO RE-INVOKE FUNCTION-------------------// var compare=function(choice1,choice2){ if (choice1 !== "rock" || "paper" || "scissors"){ var newChoice=prompt("Wrong option! Do you want to choose again?"); if (newChoice === "yes"){ alert("great"); compare(userChoice,computerChoice); //return; //I want it to re-execute the initial function - instead it loops //back to the newChoice prompt. Is it b/c compare(); is not fully defined //at this point? } else{ alert("Goodbye");} }

//——————-END LANGUAGE RE-INVOKING FUNCTION—————–//

else if (choice1 === choice2) { return "The result is a tie! Go again?"; } else if (choice1 === "rock"){ if (choice2 === "scissors"){ return "rock wins"; } else { return "paper wins";} } else if (choice1 === "paper"){ if (choice2 === "rock"){ return "paper wins";} else{ return "scissors wins";} } else if(choice1 === "scissors"){ if(choice2==="rock"){ return "rock wins";} else{ return "scissors wins";} } }; compare(userChoice,computerChoice);

Thank you guys!!

Answer 557dd5c193767687b700037f

0 votes

Permalink

I used the following:

var userChoice do{ userChoice = prompt(“What do you choose”) } while(!(userChoice == “rock” || userChoice == “scissors” || userChoice == “paper”))

that will make the prompt over and over again until the user typed either rock, scissors or paper.

Profile image of anonymousSubmitted by anonymousover 10 years

1 comments

Profile image of netherlands2Submitted by netherlands2over 10 years

Tried it and it worked! AND - figured out why it wasn’t working the way I wanted it to … the solution was so small/simple/obvious that I kind of want to hit myself right now. - I was telling the computer to repeat the function ‘compare’ - when I was supposed to be telling it to repeat/reset the variable ‘userChoice’.

Popular free courses

  • Free course

    Learn SQL

    In this SQL course, you'll learn how to manage large datasets and analyze real data using the standard data management language.
    • Checker DenseBeginner Friendly.4 Lessons
  • Free course

    Learn JavaScript

    Learn how to use JavaScript — a powerful and flexible programming language for adding website interactivity.
    • Checker DenseBeginner Friendly.11 Lessons
  • Free course

    Learn HTML

    Start at the beginning by learning HTML basics — an important foundation for building and editing web pages.
    • Checker DenseBeginner Friendly.6 Lessons
Explore full catalog

Tag » How To Restart Loop C++