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 – How can I get a prompt information and use it inside a function in JavaScript?
JavaScript

Javascript – How can I get a prompt information and use it inside a function in JavaScript?

Admin
Last updated: 2023/12/20 at 4:34 PM
Admin
Share
3 Min Read
How can I get a prompt information and use it inside a function in JavaScript?

Problem:

I’m trying to create a rock, paper and scissors game using JavaScript, in the game I need to get a user choice (rock, paper or scissors) and a random computer choice (rock, paper or scissors), compare these choices and show a winner. But I can’t get the user choice to compare with the computer choice.

Contents
Problem:Solution:

I create a function getUserChoice() with a prompt to get a user choice and use the choice inside the function playRound () but the program don’t show the prompt on the scream.

How can I fix this? and if I fix this the app will work?

<script>
            
            function getUserChoice() {
                let userChoice = window.prompt('rock, paper or scissors?')
            }
           
                       
            function getComputerChoice() {
                let computerChoice = Math.random; 
                    if (computerChoice < 0.34){return 'rock';} 
                    else if (computerChoice <= 0.67){return 'paper';}
                    else {return 'scissors';}
                
            }
                            
            
            function playRound(userChoice , computerChoice) {

                const playerSelection = getUserChoice();
                const computerSelection = getComputerChoice();
                
                    if (playerSelection === 'rock' && computerSelection === 'scissors') {
                    console.log('You win! Rock beats Scissors')
                    }
                    else if (playerSelection === 'scissors' && computerSelection === 'paper') {
                        console.log('You win! Scissors beats Paper')
                    }
                    else if(playerSelection === 'paper' && computerSelection === 'rock') {
                        console.log('You win! Paper beats Rock')
                    }
                    
                    else if(playerSelection === 'scissors' && computerSelection === 'rock') {
                        console.log('You lose! Rock beats Scissors')
                    }
                    
                    else if(playerSelection === 'paper' && computerSelection === 'scissors') {
                        console.log('You lose! Scissors beats Paper')
                    }
                    
                    else if (playerSelection === 'rock' && computerSelection === 'paper') {
                        console.log('You lose! Paper beats Rock')
                    }
                    
                    else {
                        console.log('Tie')
                    }
                }               
    </script>

Solution:

Ok there are 3 issues here.

first of all getUserChoice doesn’t return anything.

secondly you haven’t called Math.random in getComputerChoice but made a reference to that Math.random method.

and lastly you haven’t called the playRound() function.

so if we fix these issues, your code should work just fine.

            function getUserChoice() {
                return window.prompt('rock, paper or scissors?')
            }
           
                       
            function getComputerChoice() {
                let computerChoice = Math.random(); 
                    if (computerChoice < 0.34){return 'rock';} 
                    else if (computerChoice <= 0.67){return 'paper';}
                    else {return 'scissors';}
                
            }
                            
            
            function playRound(userChoice , computerChoice) {

                const playerSelection = getUserChoice();
                const computerSelection = getComputerChoice();
                
                    if (playerSelection === 'rock' && computerSelection === 'scissors') {
                    console.log('You win! Rock beats Scissors')
                    }
                    else if (playerSelection === 'scissors' && computerSelection === 'paper') {
                        console.log('You win! Scissors beats Paper')
                    }
                    else if(playerSelection === 'paper' && computerSelection === 'rock') {
                        console.log('You win! Paper beats Rock')
                    }
                    
                    else if(playerSelection === 'scissors' && computerSelection === 'rock') {
                        console.log('You lose! Rock beats Scissors')
                    }
                    
                    else if(playerSelection === 'paper' && computerSelection === 'scissors') {
                        console.log('You lose! Scissors beats Paper')
                    }
                    
                    else if (playerSelection === 'rock' && computerSelection === 'paper') {
                        console.log('You lose! Paper beats Rock')
                    }
                    
                    else {
                        console.log('Tie')
                    }
                }          
                
                playRound();

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 Most efficient way to get width of divs inside a container Javascript – Most efficient way to get width of divs inside a container
Next Article How does a method of a map apply to each element when using something like .index of Javascript – How does a method of a map apply to each element when using something like .index of
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?