9. What next?


#1


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


Hi,

I've designed this to account for an invalid entry choice, and used functions to tell the computer how to make a choice, how to compare userChoice and computerChoice, and what to do in ties. My code is working in every case without a tie. When there is a tie, it will prompt for another choice as designed, and will write the new choices to the console, but will not run the compare function and declare a winner.

What's going on here? Am I making a mistake with global/local scope of variables? Thank you for your time.


var computerChoice = ''
function computerShoots() {
    computerChoice = Math.random();
    if (computerChoice < 0.34) {
	    computerChoice = "rock";
    } else if(computerChoice <= 0.67) {
	    computerChoice = "paper";
    } else {
	    computerChoice = "scissors";
    } console.log("Computer: " + computerChoice);
}
function tie () {
    userChoice = prompt('You tied with the computer. Time for a rematch!');
    console.log('User: ' + userChoice);
    computerShoots();
    compare(userChoice, computerChoice);
}
function compare(choice1, choice2) {
    if (choice1 === choice2) {
        return tie ();
    } 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';
        }
    }
    if (choice1 !== 'rock' && choice1 !== 'paper' && choice1 !== 'scissors') {
        return 'Please pick either rock, paper, or scissors foolio.';
    }
}
var userChoice = prompt("Do you choose rock, paper, or scissors?");
console.log('User: ' + userChoice);
computerShoots();
compare(userChoice, computerChoice);


#2

I just ran your code and it logged this to the console and passed

User: rock
Computer: paper
"paper wins"

I'm not sure what you mean is wrong


#3

Hi,

Thanks for the look. In the case of a tie, the userChoice and computerChoice will log to console, but my compare function doesn't run- a winner's not declared. Any ideas?


#4

well in your tie function there is nothing telling the program to say who the winner is. so you might have to do if/else if/ else statements to have it tell you who won again, otherwise this is an Amazing program. Mine looks really lame compared to yours.


#5

Hey that's very kind of you to say. Regarding the tie function not telling the program who the winner is- I'm calling the compare function inside the tie function, and the compare function has stuff for declaring a winner. This doesn't count for telling who the winner is? Is it a matter of scope?


#6

hold on I'm trying to see if I can get it to work.


#7

Understood. Please excuse me, I didn't mean to be rude.

I searched through other replies and found something similar to mine and got it to work by editing the tie function to return the compare function.

Like this:

function tie () {
    userChoice = prompt('You tied with the computer. Time for a rematch!');
    console.log('User: ' + userChoice);
    computerShoots();
    **return compare(userChoice, computerChoice);**

Everything works now, but I'm not quite sure why a return statement was needed.


#8

Oh sorry I didn't mean to sound like that. you were fine I was just taking a while.

in the tie function there wasn't anything that showed what the result was, as far as I saw. I just used the same way to get the answer you had wanted but at least you figured it out :smiley: really neat code BTW. it was really fun to look through and see what was happening.


#9

Thanks a lot for taking the time to help me out. Much appreciated. Feels good.


#10

No problem. if you ever need help don't hesitate. No matter how small the problem is there are lots of people who are willing to help and I'm one of them. Good luck with the rest of the course! :+1:


#11

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