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

121
Views
Javascript no se vincula a HTML: juego de piedra, papel o tijera

Estoy trabajando en OdinProject pero estoy atascado en la tarea de piedra, papel o tijera. El juego se ve bien en el navegador, pero las instrucciones del archivo JS no parecen funcionar.

Intenté mover los archivos a un directorio diferente, renombrar el archivo JS y colocar el código fuente en varias partes del archivo. También he usado el depurador en las herramientas de desarrollo. El depurador dice "redelaration of userChoice" y una búsqueda en Google de este error sugiere cambiar el nombre de la variable, pero no estoy seguro de por qué es necesario si esta es la primera declaración.

Aquí está el código HTML

 <html lang="en" dir="ltr"> <head> <meta charset="utf-8"> <script type='text/javascript' src='http://ajax.googleapis.com/ajax/libs/jquery/1.10.1/jquery.min.js'></script> <script type='text/javascript' src="script.js"></script> <title>Rock Paper Scissors</title> </head> <body> <h2>Computer Choice: <span id="computer-choice"></span></h2> <h2>Your Choice: <span id="user-choice"></span></h2> <h2>Result: <span id="result"></span></h2> <button id="rock">rock</button> <button id="paper">paper</button> <button id="scissors">scissors</button> </body> </html>

Y el archivo JS

 const userChoiceDisplay = document.getElementById('user-choice') const resultDisplay = document.getElementById('result') const possibleChoices = document.querySelectorAll('button') let userChoice let computerChoice let result possibleChoices.forEach(possibleChoice => possibleChoice.addEventListener('click', (e) => { userChoice = e.target.id userChoiceDisplay.innerHTML = userChoice generateComputerChoice() getResult() })) function getResult() { if (computerChoice === userChoice) { result = 'its a draw!' } if (computerChoice === 'rock' && userChoice === "paper") { result = 'you win!' } if (computerChoice === 'rock' && userChoice === "scissors") { result = 'you lost!' } if (computerChoice === 'paper' && userChoice === "scissors") { result = 'you win!' } if (computerChoice === 'paper' && userChoice === "rock") { result = 'you lose!' } if (computerChoice === 'scissors' && userChoice === "rock") { result = 'you win!' } if (computerChoice === 'scissors' && userChoice === "paper") { result = 'you lose!' } resultDisplay.innerHTML = result }

¡Gracias por tu ayuda!

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

0

La función generarComputerChoice() no está definida.

Puedes probar esta función:

 function generateComputerChoice() { const choices = ["rock", "paper", "scissors"]; const selection = Math.round(Math.random() * 2); computerChoice = choices[selection]; }

enlace de codepen: https://codepen.io/Labnan/pen/NWaEzQW

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!