Home > other >  Loop prompt if a value is empty / wrong?
Loop prompt if a value is empty / wrong?

Time:01-13

I'm doing a simple rock, paper, exercise on console (from The Odin Project) and I am stucked in some point. I want the prompt that selects rock, paper or scissors to keep showing if the user input is empty or wrong (if is a different string from the variable choices) and I'm not getting the reuslt I want, the alert shows, yes, but it shows whether if you input a correct value, a wrong value or if you left it empty and the prompt also finishes after the loop for ends which I only want to finish if the input is "rock", "paper" or "scissors".

Can someone enlight me here? I'm just blank.

const choices = ["rock", "paper", "scissors"];
let playerScore = 0;
let computerScore = 0;
let playerName = "";

function getComputerChoice() {
    let random = choices[Math.floor(Math.random() * choices.length)];
    console.log(random);
    return random;
}


function playRound(playerSelection, computerSelection) {
    
    
    if (playerSelection === computerSelection) {
        return "It's a tie";
    } else if (playerSelection === "rock") {
        if (computerSelection === "scissors") {
            playerScore  ;
            return `${playerName} wins! ${playerSelection} beats ${computerSelection}!`;
        } else if (computerSelection === "paper") {
            computerScore  ;
            return `${playerName} lose! ${computerSelection} beats ${playerSelection}!`;
        }
    } else if (playerSelection === "paper") {
        if (computerSelection === "rock") {
            playerScore  ;
            return `${playerName} wins! ${playerSelection} beats ${computerSelection}!`;
        } else if (computerSelection === "scissors") {
            computerScore  ;
            return `${playerName} lose! ${computerSelection} beats ${playerSelection}!`;
        }
    } else if (playerSelection === "scissors") {
        if (computerSelection === "paper") {
            playerScore  ;
            return `${playerName} wins! ${playerSelection} beats ${computerSelection}!`;
        } else if (computerSelection === "rock") {
            computerScore  ;
            return `${playerName} lose! ${computerSelection} beats ${playerSelection}!`;
        }
    }
    
}

function playerChoice() {
    const userChoice = prompt("Rock, paper, scissors").toLowerCase();

    if (userChoice == null || userChoice !== choices) {
        alert("Empty or wrong choice!");
    } else if (choices.includes(userChoice)) {
        return userChoice;
    }
}

function getPlayerName() {
    playerName = prompt("What is your name?");
    return playerName;
}

function game () {
    getPlayerName();
    console.log(`Welcome ${playerName}`)

    for (i = 0; i < 3; i  ) {
        const computerSelection = getComputerChoice();
        const playerSelection = playerChoice();
        console.log(playRound(playerSelection, computerSelection));
    }
    
    if (playerScore > computerScore) {
        return "Game over! Player wins!";
    } else if (playerScore < computerScore) {
        return "Game over! Player loses!";
    } else {
        return "Game over, draw!";
    }
}

console.log(game());

Thank you very much.

CodePudding user response:

The problem is you're checking whether the input is the same as the array choices. using the include function here will check that the user's choice is in the array

const choices = ["rock", "paper", "scissors"];
let playerScore = 0;
let computerScore = 0;
let playerName = "";

function getComputerChoice() {
    let random = choices[Math.floor(Math.random() * choices.length)];
    console.log(random);
    return random;
}


function playRound(playerSelection, computerSelection) {
    
    
    if (playerSelection === computerSelection) {
        return "It's a tie";
    } else if (playerSelection === "rock") {
        if (computerSelection === "scissors") {
            playerScore  ;
            return `${playerName} wins! ${playerSelection} beats ${computerSelection}!`;
        } else if (computerSelection === "paper") {
            computerScore  ;
            return `${playerName} lose! ${computerSelection} beats ${playerSelection}!`;
        }
    } else if (playerSelection === "paper") {
        if (computerSelection === "rock") {
            playerScore  ;
            return `${playerName} wins! ${playerSelection} beats ${computerSelection}!`;
        } else if (computerSelection === "scissors") {
            computerScore  ;
            return `${playerName} lose! ${computerSelection} beats ${playerSelection}!`;
        }
    } else if (playerSelection === "scissors") {
        if (computerSelection === "paper") {
            playerScore  ;
            return `${playerName} wins! ${playerSelection} beats ${computerSelection}!`;
        } else if (computerSelection === "rock") {
            computerScore  ;
            return `${playerName} lose! ${computerSelection} beats ${playerSelection}!`;
        }
    }
    
}

function playerChoice() {
    const userChoice = prompt("Rock, paper, scissors").toLowerCase();
    console.log(choices.includes(userChoice))
    if (userChoice == null || !choices.includes(userChoice)) {
        alert("Empty or wrong choice!");
    } else if (choices.includes(userChoice)) {
        return userChoice;
    }
}

function getPlayerName() {
    playerName = prompt("What is your name?");
    return playerName;
}

function game () {
    getPlayerName();
    console.log(`Welcome ${playerName}`)

    for (i = 0; i < 3; i  ) {
        const computerSelection = getComputerChoice();
        const playerSelection = playerChoice();
        console.log(playRound(playerSelection, computerSelection));
    }
    
    if (playerScore > computerScore) {
        return "Game over! Player wins!";
    } else if (playerScore < computerScore) {
        return "Game over! Player loses!";
    } else {
        return "Game over, draw!";
    }
}

console.log(game());

Edit: Also you might want to change this line as a null input won't have the attribute .toLowerCase()

const userChoice = prompt("Rock, paper, scissors").toLowerCase();

  • Related