Soy muy nuevo en Javascript y he hecho un juego de piedra, papel o tijera. El juego en sí funciona pero nunca termina. He intentado agregar varios grados de dificultad permitiendo diferentes movimientos por nivel de dificultad. Sin embargo, ahora no tengo ideas sobre cómo hacer esto. He intentado algunas cosas diferentes y ahora estoy completamente atascado, cualquier otra cosa que parezca intentar solo hace más un desastre. Cualquier ayuda sería apreciada,
Gracias
function selectDifficulty() { let difficultyRating = ["easy", "normal", "hard"]; let easyMoves = 7; let normalMoves = 5; let hardMoves = 3; if (difficultyRating == "easy") { movesCounter = easyMoves; } else if (difficultyRating == "hard") { movesCounter = hardMoves; } else movesCounter = normalMoves; document.getElementById("moves-counter").innerHTML = movesCounter; } <!-- choose difficulty --> <div class="difficulty-area"> <h2>Choose Difficulty</h2> <button onclick="selectDifficulty('easy')" class="btn btn-easy" aria-label="Easy Difficulty"> <p id="easy">Easy</p> </button> <button onclick="selectDifficulty('normal')" class="btn btn-normal" aria-label="Normal Difficulty"> <p id="medium">Normal</p> </button> <button onclick="selectDifficulty('hard')" class="btn btn-hard" aria-label="Hard Difficulty"> <p id="hard">Hard</p> </button> </div> <!-- moves remaining until game over --> <div class="moves-remaining"> <p> Moves remaining : <span id="moves-counter">0</span> </p> </div>Las llamadas a su función usan parámetros, pero la definición de su función no los detecta.
Agregue un parámetro a la definición de la función (y elimine la matriz ahora desaparecida), y su código funcionará.
function selectDifficulty(difficultyRating) { let easyMoves = 7; let normalMoves = 5; let hardMoves = 3; if (difficultyRating == "easy") { movesCounter = easyMoves; } else if (difficultyRating == "hard") { movesCounter = hardMoves; } else movesCounter = normalMoves; document.getElementById("moves-counter").innerHTML = movesCounter; } <!-- choose difficulty --> <div class="difficulty-area"> <h2>Choose Difficulty</h2> <button onclick="selectDifficulty('easy')" class="btn btn-easy" aria-label="Easy Difficulty"> <p id="easy">Easy</p> </button> <button onclick="selectDifficulty('normal')" class="btn btn-normal" aria-label="Normal Difficulty"> <p id="medium">Normal</p> </button> <button onclick="selectDifficulty('hard')" class="btn btn-hard" aria-label="Hard Difficulty"> <p id="hard">Hard</p> </button> </div> <!-- moves remaining until game over --> <div class="moves-remaining"> <p> Moves remaining : <span id="moves-counter">0</span> </p> </div>