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

234
Views
How do I convert a number to a string?

I'm trying to create the rock, paper, scissors task from The Odin Project. When I run the code, the computerPlay function name shows a random number from 0-2 because of the Math.random function. How do I turn 0-2 into the choices ['rock', 'paper', 'scissors']?

let playerScore = 0;
let computerScore = 0;

const choices = ['rock', 'paper', 'scissors'];

function computerPlay() {
    let computerResult = [Math.floor(Math.random() * choices.length)];
    return computerResult;}

console.log(`Player Score: ${playerScore}`);
console.log(`Computer Score: ${computerScore}`);
console.log(computerPlay());

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

0

let playerScore = 0;
let computerScore = 0;

const choices = ['rock', 'paper', 'scissors'];

function computerPlay() {
    let computerResult = [Math.floor(Math.random() * choices.length)];
    return choices[computerResult];
}

console.log(`Player Score: ${playerScore}`);
console.log(`Computer Score: ${computerScore}`);
console.log(computerPlay());

I think you mean this. The computerResult would be used as the index of the choices array. (0 => rock, 1 => paper, 2 => scissors)

So, you return the value of the array at that index.

Alternative version without passing an array index (which also works):

let playerScore = 0;
let computerScore = 0;

const choices = ['rock', 'paper', 'scissors'];

function computerPlay() {
    return choices[Math.floor(Math.random() * choices.length)];
}

console.log(`Player Score: ${playerScore}`);
console.log(`Computer Score: ${computerScore}`);
console.log(computerPlay());

about 4 years ago · Juan Pablo Isaza Report

0

Just add choices let computerResult = choices[Math.floor(Math.random() * choices.length)];

try to run the code below

let playerScore = 0;
let computerScore = 0;

const choices = ['rock', 'paper', 'scissors'];

function computerPlay() {
    let computerResult = choices[Math.floor(Math.random() * choices.length)];
    return computerResult;}

console.log(`Player Score: ${playerScore}`);
console.log(`Computer Score: ${computerScore}`);
console.log(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!