I am trying to solve the problem that finds if a word is a reserved keyword or not. Unfortunately, I am stuck at it. Debugging it shows that the loop isn't incrementing, but I don't know why? Also, It looks like that only the 0th index is being compared with strLowerCase. Why is it so? I am confused.
problem statement: One programming language has the following keywords that cannot be used as identifiers: break, case, continue, default, defer, else, for, func, goto, if, map, range, return, struct, type, var
Write a program to find if the given word is a keyword or not
function wordIsTheKey(str) {
let strLowerCase = str.toLowerCase();
const keywords = [
"break",
"case",
"continue",
"default",
"defer",
"else",
"for",
"func",
"goto",
"if",
"map",
"range",
"return",
"struct",
"type",
"var",
];
keywordsLength = keywords.length;
for (let i = 0; i < keywordsLength; i++) {
if (keywords[i] == strLowerCase) {
return `${str} is a keyword`;
} else {
return `${str} is not a keyword`;
}
}
}
console.log(wordIsTheKey("defer"));
console.log(wordIsTheKey("While"));