0

we are in our 3 week of our Bootcamp. Project is to code the Hangman game, it is running in the terminal. We got preety far in just 2 days I think. We are now stucked on the case insensesetivity, meaning if the word we are looking for is Java that if you type j or J, you still would get the same result J_ _ _. I looked all over the Internet and found the locale.compare method which isn´t working for me. If i type a wrong letter everything is fine but if I type a right one I get a error and also it doesn´t ignore the case of the letter. We are not using functions yet because we haven´t learned how they work.

const constants = require('./constants');
// In node.js: install a prompt library by running: `npm install prompt-sync` in the current folder
const prompt = require("prompt-sync")();

// Here you see an example how to get your
// constants from constants.js
/*for(let figure of constants.HANGMAN_PICS)
{
   console.log(figure);
}
*/
let Answer = [];

var Words = constants.WORDS_TO_GUESS[Math.floor(Math.random() * constants.WORDS_TO_GUESS.length)];

/*console.log(Words.length);                 //Wortlänge von random Word
 */

for (let i = 0; i < Words.length; i++) {
  Answer[i] = "_";
}

console.log(Answer.join(" "));


for (; Answer !== Words;) {
  input = prompt(`Finde das Wort.`);

  if (Words.includes(input)) {
    for (let i = 0; i < Words.length; i++) {
      if (Words[i] === input) {
        Answer[i] = input;
      }
    }

    console.log(Answer.join(" "));
    console.log(Answer.localeCompare(Words, {
      sensitivity: `base`
    }));

  } else if (!Words.includes(input)) {
    console.log("Falsche Eingabe - keep trying!");
    console.log(Answer.join(" "))
  }

}

// how to use the prompt - e.g.:
// const name = prompt('What is your name?');
VLAZ
  • 26,331
  • 9
  • 49
  • 67
B. N. B
  • 41
  • 4
  • 1
    Does this answer your question? [How to do case insensitive string comparison?](https://stackoverflow.com/questions/2140627/how-to-do-case-insensitive-string-comparison) – NotMe Sep 15 '22 at 01:35

2 Answers2

1

my way of doing this (which may not be the best) is setting both strings to lower case and comparing them with includes() function. The for loop afterwards will just collect right position/s that you want to show up after right guess.

const word = 'JavaScript'
const char = 's' //input
const indexes = [] //output

//https://developer.mozilla.org/ru/docs/Web/JavaScript/Reference/Global_Objects/Array/includes 
//includes() function
if(word.toLowerCase().includes(char.toLowerCase())){ 
   //if you need to get the index/es of the latter/s 
   for (let i = 0; i < word.toLowerCase().length; i++) {
      if (word.toLowerCase()[i] === char.toLowerCase()) {
        indexes.push(i)
      }
   }
}

console.log(indexes)
0

Make sure that Words (which should be word, singular and not capitalized) is in lower case (or upper case) by doing .toLocaleLowerCase() (or .toLocaleUpperCase()) at the end of where you assign it:

var Words = constants.WORDS_TO_GUESS[Math.floor(Math.random() * constants.WORDS_TO_GUESS.length)].toLocaleLowerCase();
// >>> −−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−^^^^^^^^^^^^^^^^^^^

Then make sure input is lower case (or upper case):

input = prompt(`Finde das Wort.`).toLocaleLowerCase();

At that point, you'll be comparing j and j (or J and J), so you'll find a match.


If you don't want to consistently use one capitalization in your code, it's possible to do it differently, but more complicated.

Basically, you do the .toLocaleLowerCase() (or .toLocaleUpperCase()) everywhere you do a comparison:

for (; Answer !== Words;) {
    input = prompt(`Finde das Wort.`).toLocaleLowerCase();
// Get the input in lower case −−−−−−^^^^^^^^^^^^^^^^^^^^

    if (Words.toLocaleLowerCase().includes(input)) {
//           ^^^^^^^^^^^^^^^^^^^^−−− make Words lower case for the includes call
        for (let i = 0; i < Words.length; i++) {
            if (Words[i].toLocaleLowerCase() === input) {
//                      ^^^^^^^^^^^^^^^^^^^^−− make Words[i] lower case for the ===
                Answer[i] = Words[i];
//                          ^^^^^^^^−−− use the one from Words, not the user's input
            }
        }
        // ...
    }
}
T.J. Crowder
  • 1,031,962
  • 187
  • 1,923
  • 1,875
  • If I use this code i can press J or j but if i press j the Java would be writen in lowercases so it would print out java not Java – B. N. B Sep 14 '22 at 08:46
  • @B.N.B - Your best bet is to pick the one you want to display (upper or lower case) and use that consistently (by using `.toLocaleLowerCase()` or `.toLocaleUpperCase()` depending on which one you want). It's **possible** to use mixed case, but it would look odd in a Hangman game. – T.J. Crowder Sep 14 '22 at 09:11
  • @B.N.B - I've updated the answer to show how you might do that, but I wouldn't. :-) – T.J. Crowder Sep 14 '22 at 09:26
  • Thanks for the help I now works and shows the letters in the same case as in die word file – B. N. B Sep 15 '22 at 06:48