Why won't my rock, paper, scissors code keep score?

project
game

#1


Rock Paper Scissors

It won't keep score of the results

var name= prompt("Player 1: Please enter your name");
        console.log("Welcome" + " " + name + " " + "to Matthew's ten rounds of 'Rock, Paper, Scissors Extreme!'");
        
for (var i = 0; i < 10; i++) {
    var user = prompt("Please choose ROCK, PAPER, or SCISSORS").toUpperCase();
    var comp = Math.random();
            if (comp < 0.33) {
                comp = "ROCK";
            } else if (comp < 0.67) {
                comp = "PAPER";
            } else {
                comp = "SCISSORS";
            }
    
    console.log(name + " " + "chooses" + " " + user);
    console.log("Computer chooses: " + comp);
    
    var uscore = 0;
    var cscore = 0;
    
    var uScoreMsg = name + "Score: " + uscore;
    var cScoreMsg = "Computer Score: " + cscore;
    var breaker = " ";
    
    var lossmsg = "Computer Wins";
    var winmsg = name + " " + "Wins!!";
    
    if (user === comp) {
        console.log ("You both chose the same thing... TIE!!!");
    } else {
        switch(user) {
        
            case 'ROCK':
                if (comp === "SCISSORS") {
                    console.log(winmsg);
                    uscore++;
                    console.log(uScoreMsg);
                    console.log(cScoreMsg);
                    console.log(breaker);
                } else {
                    console.log(lossmsg);
                    cscore++
                    console.log(uScoreMsg);
                    console.log(cScoreMsg);
                    console.log(breaker);
                }
                break;
        
            case 'PAPER':
               if (comp === "ROCK") {
                    console.log(winmsg);
                    uscore++;
                    console.log(uScoreMsg);
                    console.log(cScoreMsg);
                    console.log(breaker);
                } else {
                    console.log(lossmsg);
                    cscore++
                    console.log(uScoreMsg);
                    console.log(cScoreMsg);
                    console.log(breaker); 
                }
                break;
                
            case 'SCISSORS':
               if (comp === "PAPER") {
                    console.log(winmsg);
                    uscore++;
                    console.log(uScoreMsg);
                    console.log(cScoreMsg);
                    console.log(breaker);
                } else {
                    console.log(lossmsg);
                    cscore++
                    console.log(uScoreMsg);
                    console.log(cScoreMsg);
                    console.log(breaker); 
                }
                break;
            default:
                console.log(user + " " + "isn't an option. Computer Wins");
                cscore++;
                console.log(uScoreMsg);
                console.log(cScoreMsg);
                console.log(breaker);

        }
    }
}

if (uscore > cscore) {
    console.log("Congratulations! You beat the computer. Play again, and try to make it two and zero, or quit while you're ontop.");
} else if (uscore === cscore) {
    console.log("OH!!! So close. You almost beat the computer (Or The Computer almost beat you). Play again to try again.");
} else {
    console.log("Unfortunately the computer destroyed you. Feel free to try again, or know when to walk away.");
}
.


#2

@mcl93,
You will want to keep
the initialization of your uscore and cscore OUTSIDE of your FOR-loop

var name= prompt("Player 1: Please enter your name");
        console.log("Welcome" + " " + name + " " + "to Matthew's ten rounds of 'Rock, Paper, Scissors Extreme!'");
//init counters
var uscore = 0;
var cscore = 0;
//        
for (var i = 0; i < 10; i++) {
    //
    // your code EX uscore and cscore INIT
}

#3

nice! so if i understand it correctly then, does it mean that since its in the loop, it resets every time the for loop resets? What would your advice be since I changed the code to your suggestion, and it does work, yet it is delayed a loop for the correct display of score. Is there any way to correct that?

Thank you again for your insightful help!


#4

@mcl93,

function displayScore(user,comp,again) {
    if (!again) {
        console.log(
                 "\n\n\n+++++ END = SCORE +++++");
    }
    console.log("======================= ");
    console.log("== User-scored     : "+ user);
    console.log("== Computer-scored : "+ comp);
    console.log("======================= \n\n\n");
}

var name= prompt("Player 1: Please enter your name");
        console.log(
        "\tWelcome" + " " +   name + "\n " +
   "to Matthew's  'Rock, Paper, Scissors Extreme!'");

var timeToPlay = 
       parseInt(prompt("How many times you want to play ","3"),10);
//To make it secure for idiots
if (timeToPlay > 10) { timeToPlay = 10; }

var uscore = 0;
var cscore = 0;

for (var i = 0; i < timesToPlay; i++) {
    
  console.log(
        "== Round-no."+(i+1)+"/"+ timesToPlay+ " =================\n");

          //your game-code

//
//BEFORE to closing-curly-bracket-} of the FOR-loop
//
if (uscore > cscore) {
    console.log(
    "Congratulations! You beat the computer.");
} else if (uscore === cscore) {
    console.log(
        "OH!!! So close.\n"+
        "You almost beat the computer "+
        "(Or The Computer almost beat you).");
} else {
    console.log("Unfortunately the computer destroyed you.");
    //as you are destroyed you want to =break-out= of the FOR-loop
    break;
}
console.log("===================");
if ( i < timesToPlay -1 ) {
    displayScore(uscore,cscore,true);
    console.log(
            "Play again, and try to make it two and zero,"+
            " or quit while you're ontop.\n");
}
//END of FOR loop code-block
// i is incremented by 1 and =condition= i < timesToPlay is tested
}
displayScore(uscore,cscore,false);

#5

awesome!! thanks for the help. If you don't mind me asking, how long have you been doing this for? There seems to be some stuff that isn't covered, and it might take me a little bit to understand how it works, but clearly you are quite experienced. I have just picked up this hobby in the past two months, and seeing people like you who has the ability to read programs so effortlessly and fix and create inspires me to keep pushing through because I want to get to that point at some time.

I appreciate you taking time out of your day to help an aspiring programming hobbyist.

Matthew