Question:
Passing a function in another function and make a score counter in JS

Problem

I am working on a coding task to coding a Rock, Paper, Scissors game. I have written some functions already to handle computer choice and user choice, and I'm also supposed to write a function to call the other functions and also keep track of the scores.

I am stuck without a way of bringing everything together. This is my code below.


let input;

let random;


function getComputerChoice(min = 1, max = 3){

    min = Math.ceil(min);

    max = Math.floor(max);

    random = Math.floor(Math.random() * (max - min + 1) + min);


    if (random === 1){

        console.log("computer picked Rock");

        return random;

    }

    else if (random === 2){

        console.log("computer picked Paper");

        return random;

    }

    else

        random === 3;

        console.log("computer picked Scissors");

        return random;


}



function playerSelection(){

    input = prompt("Input Rock, Paper or Scissors").toLowerCase();  

    console.log("you picked", input);

    return input;



function playRound(yourSelection, computerSelection) {

    // your code here!

    

    if (yourSelection === "rock" && computerSelection === 3){

        console.log("you win")

    }

    else if (yourSelection === "scissors" && computerSelection === 2){

        console.log("you win")

    }

    else if (yourSelection === "paper" && computerSelection === 1){

        console.log("you win")

    }

    else

        console.log("You lose, try again")

}

   

const yourSelection = playerSelection();

const computerSelection = getComputerChoice();

playRound(yourSelection, computerSelection);


function game(){

    var theGame = game()

}


Solution

In your game function you'd need a loop to run a round and save the scores. If you want to end the game enter "exit".


function getComputerChoice(min = 1, max = 3) {

  min = Math.ceil(min);

  max = Math.floor(max);

  let random = Math.floor(Math.random() * (max - min + 1) + min);


  if (random === 1) {

    console.log("computer picked Rock");

    return random;

  } else if (random === 2) {

    console.log("computer picked Paper");

    return random;

  } else

    random === 3;

  console.log("computer picked Scissors");

  return random;

}


function playerSelection() {

  let input = prompt("Input Rock, Paper or Scissors or exit to end the game").toLowerCase();

  console.log("you picked", input);

  return input;

}


function playRound(yourSelection, computerSelection) {

  if (yourSelection === "rock" && computerSelection === 3) {

    console.log("you win")

    return true;

  } else if (yourSelection === "scissors" && computerSelection === 2) {

    console.log("you win")

    return true;

  } else if (yourSelection === "paper" && computerSelection === 1) {

    console.log("you win")

    return true;

  } else

    console.log("You lose, try again")

  return false;

}


function game() {

  let yourScore = 0,

    computerScore = 0;

  while (true) {

    const yourSelection = playerSelection();

    if (yourSelection === 'exit') break;

    const computerSelection = getComputerChoice();

    const youWon = playRound(yourSelection, computerSelection);

    if (youWon) {

      yourScore += 1;

    } else {

      computerScore += 1;

    }

    alert(`${youWon ? 'You won!' : 'Computer won!'} your score=${yourScore}, computerScore=${computerScore}`)

  }

}

game();


Suggested blogs:

>Javascript Error Solved: Property 'id' does not exist on type 'T'

>Why highlighted table row using class not working in JavaScript?

>How to rename an object key based on the condition in JavaScript?

>How to sort an array based on another array in Javascript?

>Javascript: Modal not closing with a button


Nisha Patel

Nisha Patel

Submit
0 Answers