Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 4 of 4
  1. #1
    New to the CF scene
    Join Date
    Sep 2013
    Posts
    6
    Thanks
    0
    Thanked 0 Times in 0 Posts

    noob seeking help. looping a function

    I started learning javascript and i built a simple rock paper scissors game. problem is that i want it so that it will prompt a confirm box asking if you want to restart the game.and if you hit ok the game will loop through again and if you hit cancel it will end the code right there. ive been trying different while loops but nothing is working. i put the game in a function called play() so that it will just loop the function over and over again but its still not working. my question is how do i get this code to loop the play function over and over until i hit cancel on the confirm prompt? here is the code:

    Code:
    function play(){
    var userChoice = prompt("Do you choose rock, paper or scissors?");
    var computerChoice = Math.random();
    
    if (computerChoice < 0.34) {
    computerChoice = "rock";
    } else if(computerChoice <= 0.67) {
    computerChoice = "paper";
    } else {
    computerChoice = "scissors";
    }
    
    var compare=function(choice1, choice2){
    function restart(){ 
    var ask=confirm("would you like to play again?");
    return ask;
    }
    if(choice1 === choice2){
    alert("The result is a tie!");
    return restart();
    } 
    if(choice1 === "rock"){
    if (choice2 === "scissors"){
    alert("comp uses scissors. you win with rock.");
    return restart();
    }else if (choice2 === "paper"){
    alert("comp uses paper. you loose with rock.");
    return restart();
    }
    }
    if(choice1 === "paper"){
    if (choice2 === "rock"){
    alert("comp uses rock. you win with paper.");
    return restart();
    } else if (choice2==="scissors"){
    alert("comp uses scissors. you loose with paper.");
    return restart();
    }
    }
    if(choice1 === "scissors"){
    if (choice2 === "rock"){
    alert("comp uses rock. you loose with scissors.");
    return restart();
    } else if(choice2==="paper"){
    alert("comp uses paper. you win with scissors.");
    return restart();
    }
    }
    };
    console.log(compare(userChoice, computerChoice));
    } 
    var play= play();
    while(play===true){
    console.log(play);
    }

  • #2
    Senior Coder Logic Ali's Avatar
    Join Date
    Sep 2010
    Location
    London
    Posts
    1,028
    Thanks
    0
    Thanked 207 Times in 202 Posts
    Code:
    var play= play();
    
    while(play===true)
    {
     console.log(play);
    }
    You have assigned the return value of play to a variable called play, but the play function returns nothing (undefined).

    Another problem is that you now have two objects in the same scope called play, so when play() returns a value, the variable play no longer references the play function, so it can't be called again.

  • #3
    New to the CF scene
    Join Date
    Sep 2013
    Posts
    6
    Thanks
    0
    Thanked 0 Times in 0 Posts
    i get what your saying but what do i do to fix this? what is the code that i should write to make it work?

  • #4
    Senior Coder Logic Ali's Avatar
    Join Date
    Sep 2010
    Location
    London
    Posts
    1,028
    Thanks
    0
    Thanked 207 Times in 202 Posts
    Quote Originally Posted by skyking View Post
    i get what your saying but what do i do to fix this? what is the code that i should write to make it work?
    You could just have the loop inside the play function, otherwise make play return true if the user wants to play again.

    Code:
    function play()
    {
    
      .......
    
      return restart(); // Only use one return statement
    }
    
    
    while( play() )
    ;


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •