How Do I Restart A Function From Within Itself? - Codecademy
Maybe your like
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 votesPermalink
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.
1 comments
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
Tag » How To Restart Loop C++
-
How Can I Restart While Loop After Certain Condition (if) Satisfied In C++?
-
How To Restart Loop Again? - C++ Forum
-
How Can I Restart A Loop: C++ - For Beginners
-
C++ Loop Help To Restart To Beginning Of Program - C Board
-
Restarting A While Loop Without Finishing The Remaining Code.
-
How To Restart A Loop In Python? - Finxter
-
How To Use A While Loop To Restart A Game - Quora
-
How To Restart Void Loop If Condition Is Met - Arduino Stack Exchange
-
C++ How To Restart The Loop If User Just Presses Enter Or If The Input Is ...
-
JavaScript: Continue Statement - TechOnTheNet
-
How To Restart A For Loop In Go - Freshman.tech
-
Exit(0); Restart Loop - Programming Questions - Arduino Forum
-
How Can I Restart While Loop After Certain Condition (if) Satisfied In C++?
-
Going To Start Of A Loop - Scripting Support - DevForum | Roblox