Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

235
Vistas
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 Respuestas
Responde la pregunta

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 Denunciar

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 Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda