Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

JavaScript

Heriberto Nieves
Heriberto Nieves
2,466 Points

help with simple code

im not sure why my score and my tries arent updating correctly the code should ask for your choice and the random generator pick one for the cpu but i dont see why its not updating, is something i tried to put together to understand all i have learned so far any help would be appreciated it

function roll() {                               //random number generator 1 to 3
    return Math.floor(Math.random() * 3) + 1;
}

var cpuChoice;          // cpu picks rock paper or scissors

function engine() {         //generates rock paper or scissors for the cpu

var number = roll();

    if (number === 1) {
        cpuChoice = "ROCK";
    } else if (number === 2) {
        cpuChoice = "PAPER";
    } else if (number === 3) {
        cpuChoice = "SCISSORS";
    }
}

var score = 0;
var tries = 3;

var userChoice = prompt("ROCK PAPER OR SCISSORS").toUpperCase();

engine();

function gameOn() {
    if (cpuChoice === "ROCK" && userChoice === "PAPER") {
        score = score + 10;
    } else if (cpuChoice === "ROCK" && userChoice === "SCISSORS") {
        tries = tries - 1;
    } else if (cpuChoice === "ROCK" && userChoice === "ROCK") {
        score = score + 5;
    } else if (cpuChoice === "PAPER" && userChoice === "SCISSORS") {
        score = score + 10;
    } else if (cpuChoice === "PAPER" && userChoice === "ROCK") {
        tries = tries - 1;
    } else if (cpuChoice === "PAPER" && userChoice === "PAPER") {
        score = score + 5;
    } else if (cpuChoice === "SCISSORS" && userChoice === "ROCK") {
        score = score + 10;
    } else if (cpuChoice === "SCISSORS" && userChoice === "PAPER") {
        tries = tries -1;
    } else if (cpuChoice === "SCISSORS" && userChoice === "SCISSORS") {
        score = score + 5;
    }

}

alert(userChoice + " VS " + cpuChoice);
alert("SCORE: " + score);
alert("LIFE: " + tries);

1 Answer

Damien Watson
Damien Watson
27,419 Points

Hey, it's really close. The problem is that 'gameOn' is never called and you need to keep looping until all tries are gone.

I have moved some things into a 'while' loop at the bottom, so have a look. I have tested it and it works.

function roll() {                               //random number generator 1 to 3
    return Math.floor(Math.random() * 3) + 1;
}

var cpuChoice;          // cpu picks rock paper or scissors

function engine() {         //generates rock paper or scissors for the cpu

var number = roll();

    if (number === 1) {
        cpuChoice = "ROCK";
    } else if (number === 2) {
        cpuChoice = "PAPER";
    } else if (number === 3) {
        cpuChoice = "SCISSORS";
    }
}

var score = 0;
var tries = 3;

function gameOn() {
    if (cpuChoice === "ROCK" && userChoice === "PAPER") {
        score = score + 10;
    } else if (cpuChoice === "ROCK" && userChoice === "SCISSORS") {
        tries = tries - 1;
    } else if (cpuChoice === "ROCK" && userChoice === "ROCK") {
        score = score + 5;
    } else if (cpuChoice === "PAPER" && userChoice === "SCISSORS") {
        score = score + 10;
    } else if (cpuChoice === "PAPER" && userChoice === "ROCK") {
        tries = tries - 1;
    } else if (cpuChoice === "PAPER" && userChoice === "PAPER") {
        score = score + 5;
    } else if (cpuChoice === "SCISSORS" && userChoice === "ROCK") {
        score = score + 10;
    } else if (cpuChoice === "SCISSORS" && userChoice === "PAPER") {
        tries = tries -1;
    } else if (cpuChoice === "SCISSORS" && userChoice === "SCISSORS") {
        score = score + 5;
    }

}

while (tries > 0) {
    var userChoice = prompt("ROCK PAPER OR SCISSORS").toUpperCase();

    engine();
    gameOn();

    alert(userChoice + " VS " + cpuChoice);
    alert("SCORE: " + score);
    alert("LIFE: " + tries);
}
alert("GAME OVER!");
Heriberto Nieves
Heriberto Nieves
2,466 Points

thanks a lot, i knew i needed loops but i wanted to do it with what i have learned so far, not yet in the loop but pretty cool, im on the right track! thanks a lot