Home > OS >  I'm making a wordle like game using Javascript and I can't find out how to test to see if
I'm making a wordle like game using Javascript and I can't find out how to test to see if

Time:04-28

var userWord = readLine("Enter a 5 letter word: \n");

var randomItem = listOfWords[Math.floor(Math.random()*listOfWords.length)];

var newWord = [userWord];

if(newWord != randomItem){
    letterChecker();
} else {
    println("Correct!");
}

Checks if the first letter of both words is the same or not, I can't seem to get how to find the first character of both arrays and compare. If I type in the correct word the "Correct!" prompt displays, however the "is green" doesn't appear.

function letterChecker(){

 if (newWord[0] === randomItem[0]{
        
        println(newWord[0]  " is green"); 
    } 

}

CodePudding user response:

You don't need to put newWord into array, use directly like:

var userWord = 'hi';
const listOfWords = ['hi', 'hello'];
var randomItem = listOfWords[Math.floor(Math.random() * listOfWords.length)];

var newWord = userWord;

if (newWord != randomItem) {
  letterChecker();
} else {
  console.log("Correct!");
}

function letterChecker() {
  console.log(newWord, randomItem);
  if (newWord[0] === randomItem[0]) {
    console.log(newWord[0]   " is green");
  }

}

  • Related