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

205
Views
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 answers
Answer question

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 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!