Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

236
Visualizações
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 Respostas
Responde à pergunta

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 Relatório

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 Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda