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

174
Views
Beginners first try at coding rock, paper.. game. Keeps returning 'paper wins'

I keep getting 'paper wins' when I enter 'rock' as my choice.

function game() {
  pChoice = prompt('What is your play?');
  cChoice = ['rock','paper','scissor'];
  
  let compChoice = [Math.floor(Math.random() * cChoice.length)];
  console.log(compChoice);

  function compare(pChoice, compChoice) {
    if (pChoice === compChoice) {
      alert( "It's a tie" );
    }

    if (pChoice === 'rock') {
      if (compChoice === 'scissor') {
        return 'rock wins';
      } else {
        return 'paper wins';    
      }
    }
    else if (pChoice === 'paper') {
      if (compChoice === 'rock') {
        return 'paper wins';
      } else {
        return 'scissor wins';
      }
    }
    else if (pChoice === 'scissor') {
      if (compChoice === 'paper') {
        return 'scissor wins';
      } else {
        return 'rock wins';
      }
    }
  }

  var result = compare(pChoice, compChoice);
  console.log(compare(pChoice, compChoice));
}

game();
about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

You never assign cChoice to a value, instead you are assigning it to an array with a number in it.

For example:

// Assigning to [number]
let compChoice = [Math.floor(Math.random() * cChoice.length)];
// Assigning to string inside of array cChoice
let compChoice = cChoice[Math.floor(Math.random() * cChoice.length)];
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!