By using this site, you agree to the Privacy Policy and Terms of Use.
Accept
rocoderesrocoderes
  • Home
  • HTML & CSS
    • Login and Registration Form
    • Card Design
    • Loader
  • JavaScript
  • Python
  • Internet
  • Landing Pages
  • Tools
    • Google Drive Direct Download Link Generator
    • Word Count
  • Games
    • House Painter
Notification Show More
Latest News
How to set the dropdown value by clicking on a table row
Javascript – How to set the dropdown value by clicking on a table row
JavaScript
Attempting to increase the counter, when the object's tag exist
Javascript – Attempting to increase the counter, when the object’s tag exist
JavaScript
Cycle2 JS center active slide
Javascript – Cycle2 JS center active slide
JavaScript
Can import all THREE.js post processing modules as ES6 modules except OutputPass
Javascript – Can import all THREE.js post processing modules as ES6 modules except OutputPass
JavaScript
How to return closest match for an array in Google Sheets Appscript
Javascript – How to return closest match for an array in Google Sheets Appscript
JavaScript
Aa
Aa
rocoderesrocoderes
Search
  • Home
  • HTML & CSS
    • Login and Registration Form
    • Card Design
    • Loader
  • JavaScript
  • Python
  • Internet
  • Landing Pages
  • Tools
    • Google Drive Direct Download Link Generator
    • Word Count
  • Games
    • House Painter
Follow US
High Quality Design Resources for Free.
rocoderes > JavaScript > Javascript – Pass a function in another function and make a score counter
JavaScript

Javascript – Pass a function in another function and make a score counter

Admin
Last updated: 2024/02/10 at 1:03 PM
Admin
Share
3 Min Read
Pass a function in another function and make a score counter

Problem:

I am working on a coding task to coding a Rock, Paper, Scissors game.
I have written some functions already to handle to 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.

Contents
Problem:Solution:

I am stuck without a way of bring 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();

Expand snippet

Related

Subscribe to Our Newsletter

Subscribe to our newsletter to get our newest articles instantly!

Share this Article
Facebook Twitter Email Print
What do you think?
Love0
Sad0
Happy0
Sleepy0
Angry0
Dead0
Wink0
Previous Article How to debug "not a valid selector" without access to the offending browser page Javascript – How to debug “not a valid selector” without access to the offending browser page
Next Article Asserting Vuetify v-text-field value using Cypress Javascript – Asserting Vuetify v-text-field value using Cypress
Leave a comment Leave a comment

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

- Advertisement -

You Might Also Like

How to set the dropdown value by clicking on a table row

Javascript – How to set the dropdown value by clicking on a table row

February 11, 2024
Attempting to increase the counter, when the object's tag exist

Javascript – Attempting to increase the counter, when the object’s tag exist

February 11, 2024
Cycle2 JS center active slide

Javascript – Cycle2 JS center active slide

February 10, 2024
Can import all THREE.js post processing modules as ES6 modules except OutputPass

Javascript – Can import all THREE.js post processing modules as ES6 modules except OutputPass

February 10, 2024
rocoderesrocoderes
Follow US

Copyright © 2022 All Right Reserved By Rocoderes

  • Home
  • About us
  • Contact us
  • Disclaimer
Join Us!

Subscribe to our newsletter and never miss our latest news, podcasts etc.

Zero spam, Unsubscribe at any time.
Welcome Back!

Sign in to your account

Lost your password?