Home > Back-end >  For() loop not doing anything in the first loop
For() loop not doing anything in the first loop

Time:04-30

I am doing the 3rd project of TOP (The Odin Project) which consists of creating a simple rock paper scissors game with no GUI for the moment, just on the console. This is a pseudo-code for the loop I want to make:

prompt user for number of rounds, assign to variable rounds
start counter (i) at 0 and for the values of i up to (not including) the number of rounds, execute the following code:

I have no problem with this code, it used to work perfectly using the while loop instead of the for loop, so I guess I must have done something incorrectly.

Here is the code I've written:

let rounds = prompt("How many rounds do you want to play?")

for (let i = 0; i < rounds; i  ) {
    let playerMove = convertToNumber(promptPlayer())
    let computerMove = computerPlay()
    if (computerMove > playerMove) {
        if (computerMove === 2 && playerMove === 0) {
            console.log(`You won! ${convertToItem(playerMove)} beats ${convertToItem(computerMove)}.`)
        } else {
            console.log(`You lost! ${convertToItem(computerMove)} beats ${convertToItem(playerMove)}.`)
        }
    } else if (playerMove > computerMove) {
        if (computerMove === 0 && playerMove === 2) {
            console.log(`You lost! ${convertToItem(computerMove)} beats ${convertToItem(playerMove)}.`)
        } else {
            console.log(`You won! ${convertToItem(playerMove)} beats ${convertToItem(computerMove)}.`)
        }
    } else if (playerMove) {
    console.log("Ties!")
    }
}

And this is a detailed explanation of the page's behaviour: If I enter 5, it will prompt me for rock, paper or scissors 5 times, and each one, except for the first one (that is the problem) is logged in the console. I have no clue why the first one is omitted.

I'm not sure weather I provided enough information, if not, I will send the whole code (6 lines, not much). The rest of the code are just function declarations which I did not alter when I changed the while loop to a for loop.

Thank you.

EDIT:

I saw many comments were highlighting the insufficient code, so here is the complete code I wrote:

function computerPlay() {
    return Math.floor(Math.random() * 3)
}

function convertToItem(num) {
    switch (num) {
        case 0:
            return "rock";
        case 1:
            return "paper";
        case 2:
            return "scissors";
    }
}

function convertToNumber(item) {
    switch (item) {
        case "rock":
            return 0;
        case "paper":
            return 1;
        case "scissors":
            return 2;
    }
}

function promptPlayer() {
    let playerMove = prompt("Rock, Paper or Scissors?").toLowerCase()
    if (playerMove !== "rock" && playerMove !== "paper" && playerMove !== "scissors") {
        alert("Invalid answer! Try again.")
    } else {
        return playerMove
    } 
}

let rounds = prompt("How many rounds do you want to play?")

for (let i = 0; i < rounds; i  ) {
    let playerMove = convertToNumber(promptPlayer())
    let computerMove = computerPlay()
    if (computerMove > playerMove) {
        if (computerMove === 2 && playerMove === 0) {
            console.log(`You won! ${convertToItem(playerMove)} beats ${convertToItem(computerMove)}.`)
        } else {
            console.log(`You lost! ${convertToItem(computerMove)} beats ${convertToItem(playerMove)}.`)
        }
    } else if (playerMove > computerMove) {
        if (computerMove === 0 && playerMove === 2) {
            console.log(`You lost! ${convertToItem(computerMove)} beats ${convertToItem(playerMove)}.`)
        } else {
            console.log(`You won! ${convertToItem(playerMove)} beats ${convertToItem(computerMove)}.`)
        }
    } else if (playerMove) {
    console.log("Ties!")
    }
}

CodePudding user response:

Your loop does work. That's the reason you're getting 5 prompts. But your if-statements aren't always true, so sometimes it won't output something in the console. Especially the last one is a strange one. If everything else needs to be Ties! then just use the else instead of another else if (that might be false and won't display a message)

You're sample code is insufficient for a working example so i've made some changes below, but the loop is the same and the logic with if-statements (all thought I did change the order a bit so I didn't need nested if's).

let rounds = prompt("How many rounds do you want to play?");

for (let i = 0; i < rounds; i  ) {
    let playerMove = Number( prompt("What your gonna do? Rock=0; Paper=1; Scissors=2;") ); // Expect a number 0, 1 or 2
    let computerMove = Math.floor(Math.random() * 3) // random number 0, 1 or 2
    
    // if numbers from playerMove and computerMove are the same it's a tie
    if (playerMove === computerMove) {
        console.log("Ties!");
    } 
    // if player has Rock(0) and computer Paper(1) or player has Paper(1) and computer Scissors(2) you lose..
    // if player has Scissors(2) but computer has Rock(0) you lose as well
    else if (playerMove 1 === computerMove || playerMove-2 === computerMove) {
        console.log("You lost!");
        }
    // in all other cases you win
    else {
            console.log("You win!");
    }
    
    console.log("Played by player: "    playerMove);
    console.log("Played by computer: "   computerMove);
}

  • Related