Someone help with last step of Next Steps - where's my code wrong?


#1



https://www.codecademy.com/en/courses/javascript-beginner-en-Bthev-mskY8/1/5?curriculum_id=506324b3a7dffd00020bf661


I'm trying to alter the last step of "If it ends a tie, what if it doesn't end there?" into "If they enter an answer that isn't rock/paper/scissors/rope, the game starts over". I'm also trying to make it start over more than once if the user still doesn't enter a correct option, rather than just ending once. Maybe the problem is my moderate but incomplete training in other languages and things just don't work this way in Javascript? Anyway, I'm finally giving up on figuring this out without help. Can someone please correct my code as little as possible but make it work properly?

I don't even understand what's happening. It always prompts me at least twice, even if I put a "correct" answer both times, but sometimes it prompts me three times. I don't get it.


var decideUserChoice = function() {
    var userChoice = prompt("Do you choose rock, paper, scissors, or rope? Use lowercase letters only.");
    return userChoice;
}

var decideComputerChoice = function() {

    var computerChoice = Math.random();
    
    if (computerChoice <= 0.25) {
        computerChoice = "rock";
    } 
    else if (computerChoice <= 0.50) {
        computerChoice = "paper";
    } 
    else if (computerChoice <= 0.75) {
        computerChoice = "scissors";
    }
    else {
        computerChoice = "rope";
    }
    return computerChoice;
}

var compare = function(choice1, choice2) {
    if (choice1 === choice2) {
        return "The result is a tie!";    
    }
    else if (choice1 === "rock") {
        if (choice2 === "scissors") {
            return "Rock smashes scissors. Player wins!";  
        }
        else if (choice2 === "paper") {
            return "Paper covers rock. Computer wins!";   
        }
        else {
            return "Rope ties up rock. Computer wins!";
        }
    }
    else if (choice1 === "paper") {
        if (choice2 === "rock") {
            return "Paper covers rock. User wins!";    
        }
        else if (choice2 === "scissors") {
            return "Scissors cut paper. Computer wins!";
        }
        else {
            return "Rope and paper fall in love. Everybody wins!";
        }
    }
    else if (choice1 === "scissors") {
        if (choice2 === "rock") {
            return "Rock smashes scissors. Computer wins!";   
        }
        else if (choice2 === "paper") {
            return "Scissors cut paper. User wins!";   
        }
        else {
            return "Scissors need sharpening and can't cut rope. Game rescheduled.";    
        }
    }
    else if (choice1 === "rope") {
        if (choice2 === "rock") {
            return "Rope ties up rock. User wins!";
        }
        else if (choice2 === "paper") {
            return "Rope and paper fall in love. Everybody wins!";  
        }
        else {
            return "Scissors need sharpening and can't cut rope. Game rescheduled.";    
        }
    }
    else {
        console.log("Try again.");
        return "You must choose rock, paper, scissors, or rope and use lowercase letters only.";
        
    }
}



var runGame = function() {
    var userChoice = decideUserChoice();
    var computerChoice = decideComputerChoice();
    console.log("User: " + userChoice);
    console.log("Computer: " + computerChoice);
    var answer = compare(userChoice, computerChoice);
  	console.log(answer);
    return answer;
}

var answer = runGame();
runGame();
if (answer === "You must choose rock, paper, scissors, or rope and use lowercase letters only.") {
    runGame();   
}
else {
    console.log("Game Over");
}


#2

I spot some double work. When you assing the var answer the value of the function runGame(), the function runGame will be executed. The will be prompted an answer.
But in the next line, you execute the function runGame() again, so the user will be prompted a question again.

In short, the second runGame() call isn't needed, because when assigning a value to answer the function is already called.

The two lines I'm talking about:


#3

Yep, that was it. That seems... weird though. Should I be using separate functions for my code to make more sense? One where I can get the input and a separate one to run the compare function and output to the console? It seems like I tried to do that and could not get it to work. Ugh. Maybe I've just been staring at this too long. Thank you btw!


#4

Youre welcome! And great job on the game!

You could make a serperate function that obtains and validates the users input. That way you don't have to check afterwards and your compare function is a bit more error proof.


#5

Hey thanks! It only took me three brain aneurysms to get this far, so that's not bad. I just went to try and separate my functions and I remember at least part of the problem I was having. If you keep changing your code within the same lesson, the codecademy editor starts acting strangely - like leftover data from the code you just ran is stuck in there somewhere even if you've deleted and changed it. It also does things like printing returns to the console which it shouldn't do, but it doesn't even do that consistently(which makes my compare function hard to deal with). You might watch out for that if you're having things happen that you don't expect.


#6

Yes, I can imagine that making it very difficult to spot the real errors.
You could also use an external source to write the code in before throwing it back to codecademy and checking your answer.

Good luck and have fun with the rest of the course!


#7

Thanks much! You too!


#8

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.