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

335
Views
¿Conseguir piedra, papel o tijera?

estoy trabajando en un sencillo guión de piedra, papel o tijera para el Proyecto Odín como parte del curso; cada respuesta que obtengo es hacia cuando lo ejecuto en la consola ("Draw") sin importar lo que ingrese; ¿Qué estoy haciendo mal?

Aquí está el enlace a la tarea: sé que si eliminé las diversas funciones, lo más probable es que pueda hacer que funcione, pero el resumen es usar estas funciones específicas, de ahí el problema.

Creo que el problema podría estar en la función playerSelection, pero no puedo estar seguro. Agradecería un poco de orientación.

Enlace del proyecto Odin: https://www.theodinproject.com/lessons/foundations-rock-paper-scissors

 let choice = ["Rock", "Paper", "Scissors"]; function computerPlay() { let choice = ["Rock", "Paper", "Scissors"]; return randomChoice = choice[Math.floor(Math.random() * choice.length)]; } function playerSelection() { let playerChoice = prompt("Enter Rock or Paper or Scissors"); } function playRound(computerPlay, playerSelection) { if (computerPlay === "Rock" && playerSelection === "Scissors") { return "Computer wins"; } else if (computerPlay === "Paper" && playerSelection === "Rock") { return "Player wins"; } else if (computerPlay === "Scissors" && playerSelection === "Paper") { return "Computer wins"; } else if (playerSelection === "Rock" && computerPlay === "Scissors") { return "Computer wins"; } else if (playerSelection === "Paper" && computerPlay === "Rock") { return "Player wins"; } else if (playerSelection === "Scissors" && computerPlay === "Paper") { return "Player wins"; } else { return "Draw"; } } playerSelection() const computerSelection = computerPlay(); console.log(playRound(playerSelection, computerSelection));

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

0

Tienes algunos errores:

  1. No llamas a la función playerSelection en console.log()
  2. No devuelve valor de la función playerSelection()
  3. Tiene dos argumentos en la función playRound (1: computerPlay, 2: playerSelection) pero llama a playRound con un orden de argumentos desordenado.
  4. Tienes una llamada extra para la función playerSelection()

 //let choice = ["Rock", "Paper", "Scissors"]; function computerPlay() { let choice = ["Rock", "Paper", "Scissors"]; return randomChoice = choice[Math.floor(Math.random() * choice.length)]; } function playerSelection() { // 2) add return return prompt("Enter Rock or Paper or Scissors"); } // 3) change order for arguments function playRound(playerSelection, computerPlay) { console.log(`Computer choose ${computerPlay}`); if (computerPlay === "Rock" && playerSelection === "Scissors") { return "Computer wins"; } else if (computerPlay === "Paper" && playerSelection === "Rock") { return "Player wins"; } else if (computerPlay === "Scissors" && playerSelection === "Paper") { return "Computer wins"; } else if (playerSelection === "Rock" && computerPlay === "Scissors") { return "Computer wins"; } else if (playerSelection === "Paper" && computerPlay === "Rock") { return "Player wins"; } else if (playerSelection === "Scissors" && computerPlay === "Paper") { return "Player wins"; } else { return "Draw"; } } // 4) remove playerSelection call // playerSelection() const computerSelection = computerPlay(); // 1) add call to playerSelection() function console.log(playRound(playerSelection(), computerSelection));

PD: tienes que usar el objeto para encontrar al ganador:

 function computerPlay() { let choice = ["Rock", "Paper", "Scissors"]; return randomChoice = choice[Math.floor(Math.random() * choice.length)]; } function playerSelection() { return prompt("Enter Rock or Paper or Scissors"); } function playRound(playerSelection, computerPlay) { console.log(`player: ${playerSelection} VS + computer: ${computerPlay}`); if (playerSelection === computerPlay) return 'Draw' const winPairs = { Paper: 'Rock', Scissors: 'Paper', Rock: 'Scissors' } return winPairs[playerSelection] === computerPlay ? 'Player wins' : 'Computer wins' } console.log(playRound(playerSelection(), computerPlay()));

about 4 years ago · Juan Pablo Isaza Report

0

Como escribieron los demás, su indicador nunca "aterriza" en la cadena if , de modo que cada vez que se llama a la última declaración else .

La forma más sencilla sería omitir las otras dos funciones e insertar sus dos líneas necesarias en la función playRound() .

Ejemplo de trabajo:

 const choice = ["Rock", "Paper", "Scissors"]; function playRound() { const computerSelection = choice[Math.floor(Math.random() * choice.length)]; const playerSelection = prompt("Enter Rock or Paper or Scissors"); if (computerSelection === "Rock" && playerSelection === "Scissors") { return "Computer wins"; } else if (computerSelection === "Paper" && playerSelection === "Rock") { return "Player wins"; } else if (computerSelection === "Scissors" && playerSelection === "Paper") { return "Computer wins"; } else if (playerSelection === "Rock" && computerSelection === "Scissors") { return "Computer wins"; } else if (playerSelection === "Paper" && computerSelection === "Rock") { return "Player wins"; } else if (playerSelection === "Scissors" && computerSelection === "Paper") { return "Player wins"; } else { return "Draw"; } } console.log(playRound());

about 4 years ago · Juan Pablo Isaza Report

0

La función playerSelection no devuelve la selección, sino que simplemente declara una variable con la salida de la función de prompt como su salida. Deberías devolver la variable así:

 function playerSelection() { let playerChoice = prompt("Enter Rock or Paper or Scissors"); return playerChoice }

Por eso vuelve "Draw"

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!