Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

228
Views
How to use output of prompt as global variable in another function

Following Odin Project Rock Paper Scissors - trying to use a JS prompt input in another function to work out who won a game.

Have stripped a lot out to hopefully make the question/answer as reusable as possible for others.

I have 2 global variables set :

let computerSelection;
let playerSelection;

I have a function (playRound) which works out the outcome of a round (who won) which I know works when hard coding the player selection (as I've tried console logging the output successfully).

However I want to replace the hard coding of the player selection to use the input of a prompt. But when I do this, it isn't setting the playerSelection properly as I'm just getting 'undefined' when I console log playRound().

My prompt :

function showModal() {
    selection = window.prompt('Rock, paper or scissors?').toLowerCase(); 
    playerSelection=selection
}

I know this works when I console log the playerSelection with a different button like this:

function print() {
    console.log(playerSelection);
}

however, when I try use it in my playRound() function it is showing undefined?

function playRound(playerSelection, computerSelection) {
  computerSelection = computerPlay();
  playerSelection = showModal(playerSelection); 

I've also tried just using

playerSelection = playerSelection

and also

playerSelection = showModal()

as I thought surely the global variable is set once the modal is complete but that is also returning undefined???

I don't understand why this isn't working in the playRound function as it seems the modal/prompt is setting the playerSelection variable (when I console.log playerSelection after doing it (using my test print() function?)

about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

You have an issue with scope here. Because you have a parameter called playerSelection being passed into the function it is setting that variable and not your global variable. You can solve this like this:

let playerSelection;
let computerSelection;

function playRound() {
  computerSelection = computerPlay();
  playerSelection = showModal(playerSelection);
}

function showModal() {
    return window.prompt('Rock, paper or scissors?').toLowerCase(); 
}

playRound();

Note that also you need to return the prompt result from showModal, a function without a return statement will always return undefined.

about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!