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

132
Views
javascript function returns the same value when its supposed to be random

when i call function game() it keeps returning the same value from computerSelection even when i have it set to return a random value. when i call computerPlay() by itself it will give random value each time its called but not when its used in the game() function under the variable computerSelection.

  function computerPlay() {
  const plays = ["rock", "paper", "scissors"];
  return plays[Math.floor(Math.random() * plays.length)];
};



function playRound(playerSelection, computerSelection) {
  if (playerSelection.toLowerCase() === 'rock' && computerSelection === 'paper') {
    console.log('paper beat rock')
    return ('paper beats rock')
  } else if (playerSelection.toLowerCase() === 'rock' && computerSelection === 'rock') {
    console.log('its a tie, try again!')
    return ('its a tie, try again!')
  } else if (playerSelection.toLowerCase() === 'rock' && computerSelection === 'scissors') {
    console.log('rock beats scissors')
    return ('rock beats scissors')
  } else {
    console.log('wut')
  }
};

function game() {
      playRound(playerSelection, computerSelection);
      playRound(playerSelection, computerSelection);
};



const playerSelection = "Rock";
let computerSelection = computerPlay();
about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

You should call computerPlay() once for each playRound(). So try this:

function game() {
      playRound(playerSelection, computerPlay());
      playRound(playerSelection, computerPlay());
};
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!