cryya
cryya

Reputation: 5

I'm making a wordle like game using Javascript and I can't find out how to test to see if the first letter of both comparable words are the same

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"); 
    } 

}

Upvotes: 0

Views: 113

Answers (1)

Simone Rossaini
Simone Rossaini

Reputation: 8162

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");
  }

}

Upvotes: 1

Related Questions