const listOfWords = ['angel', 'break', 'clear', 'knock', 'maybe'];
var userWord = readLine("Enter a 5 letter word: \n");
var randomItem = listOfWords[Math.floor(Math.random()*listOfWords.length)];
var newWord = userWord;
var randomItem;
letterChecker();
The "is yellow" statement should only be displayed if the letter is in the array, but not in that spot, and the "is grey" should only be placed if the character isn't in the array at all but neither work as they should. When you type in a 5 letter word, the corresponding character such as [0], [1], [2], [3], [4] should compare, however this isn't the case. For context I have the same if statement below after the other but every [] is the next number up. They are all within the same function I just didn't add it so it didn't take up space.
function letterChecker() {
println(randomItem);
if(newWord[0] === randomItem[0]) {
console.log(newWord[0] " is green");
} else {
if(newWord.includes(randomItem[0])){
console.log(newWord[0] " is yellow");
}
}
if(!newWord.includes(randomItem[0])){
console.log(newWord[0] " is grey");
}
if(newWord[1] === randomItem[1]) {
console.log(newWord[1] " is green");
} else {
if(newWord.includes(randomItem[1])){
console.log(newWord[1] " is yellow");
}
}
if(!newWord.includes(randomItem[1])){
console.log(newWord[1] " is grey");
}
if(newWord[2] === randomItem[2]) {
console.log(newWord[2] " is green");
} else {
if(newWord.includes(randomItem[2])){
console.log(newWord[2] " is yellow");
}
}
if(!newWord.includes(randomItem[2])){
console.log(newWord[2] " is grey");
}
if(newWord[3] === randomItem[3]) {
console.log(newWord[3] " is green");
} else {
if(newWord.includes(randomItem[3])){
console.log(newWord[3] " is yellow");
}
}
if(!newWord.includes(randomItem[3])){
console.log(newWord[3] " is grey");
}
if(newWord[4] === randomItem[4]) {
console.log(newWord[4] " is green");
} else {
if(newWord.includes(randomItem[4])){
console.log(newWord[4] " is yellow");
}
}
if(!newWord.includes(randomItem[4])){
console.log(newWord[4] " is grey");
}
}
The first word is the word I input into the readline() and the second word is the randomized word. This is the output:
Enter a 5 letter word:
angel
break
a is grey
Enter another word:
break
r is yellow
Enter another word:
break
e is green
Enter another word:
break
a is green
Enter another word:
break
k is green
Expexted Output:
Enter a 5 letter word:
angel
break
a is yellow
Enter another word:
break
r is green
Enter another word:
break
e is green
Enter another word:
break
a is green
Enter another word:
break
k is green
Code output:
ReferenceError: i is not defined
at letterChecker (17:10)
at 12:1
Enter a 5 letter word:
angel
randomItemknock
CodePudding user response:
I think you just have your includes
backwards; you're checking that the user's word includes a particular letter from the random word, whereas you should be checking that the random word contains the letter from the user's word.
const listOfWords = ['angel', 'break', 'clear', 'knock', 'maybe'];
//var userWord = readLine("Enter a 5 letter word: \n");
var userWord = "angel";
var randomItem = listOfWords[Math.floor(Math.random()*listOfWords.length)];
var newWord = userWord;
var randomItem;
letterChecker();
function letterChecker() {
console.log("randomItem",randomItem);
for (i = 0; i < 5; i ) {
console.log("newWord[i]",newWord[i]);
console.log("randomItem[i]",randomItem[i]);
console.log("equal?",newWord[i] === randomItem[i]);
console.log(randomItem " includes '" newWord[i] "'?",randomItem.includes(newWord[i]));
if(newWord[i] === randomItem[i]) {
console.log(newWord[i] " is green");
}
else {
if(randomItem.includes(newWord[i])){//words reversed
console.log(newWord[i] " is yellow");
}
}
if(!randomItem.includes(newWord[i])){
console.log(newWord[i] " is grey");
}
console.log("---")
}
}