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

211
Vistas
To lower case using if else/ for

I am the very begginer (only if/else, for, while, slice etc) and i ve got a problem: so i wrote Hangman game. I need to put in there code saying ‘’let’s player upper case guess letter transform to lowercase one every time he puts uppercase letter”

  1. Did i choose the right place for this new code in existing code?
  2. Were my thoughts about appropriate code more or less right?
  3. If not: what s wrong then?

var words = ["fish", "monkey", "pioni", "agreable"];
var randomWord = words[Math.floor(Math.random() * words.length)];
var answerArray = [];
for (var i = 0; i < randomWord.length; i++) {
  answerArray[i] = "_";
}
var ramainingLetters = randomWord.length;

//Game circle
while (ramainingLetters > 0) {
  alert(answerArray.join(" "));

  var guess = prompt("Guess a letter or press cancel to exit game");
  if (guess === null) {
    break;
  } else if (guess.length !== 1) {
    alert("Enter only one letter");
  } else if (guess == guess.toUpperCase()) {
    guess = guess.toLowerCase();
  } else {
    //renew game cycle
    for (var j = 0; j < randomWord.length; j++) {
      if (randomWord[j] === guess) {
        answerArray[j] = guess;
        ramainingLetters--;
      }
    }
  }

  // stop game 
}
alert(answerArray.join(" "));
alert(" Cool! this word was " + randomWord);

about 4 years ago · Juan Pablo Isaza
1 Respuestas
Responde la pregunta

0

You could easily solve your problem by converting the chosen word to uppercase and everytime the user puts in a letter, make that uppercase too.

var randomWord = words[Math.floor(Math.random() * words.length)].toUpperCase();

And convert your quess always to uppercase

guess = guess.toUpperCase();

This way everything is consistent.
If they type in a letter in lowercase its getting converted to uppercase and compared with the word also in uppercase.

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