简体   繁体   中英

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

}

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM