I'm beginning programming with codecademy and I tried to write a "search for word" program, but I don't know how to write a condition in if
using a for
loop to check all the letters in a search word, no matter the length.
I know the for loop gives me true
or false
, but I would like it to generate as many text[j] === myWord[ji]
conditions to my code as there are letters.
Is there something wrong with my syntax or should I use other command that I don't know of?
var text = "Within the field of literary criticism, text also refers to the original information content of a particular piece of writing; that is, the text of a work is that primal symbolic arrangement of letters as originally composed, apart from later alterations, deterioration, commentary, translations, paratext, etc. Therefore, when literary criticism is concerned with the determination of a text, it is concerned with the distinguishing of the original information content from whatever has been added to or subtracted from that content as it appears in a given textual document (that is, a physical representation of text).";
var myWord = "literary";
var hits = [];
for (var i=0; i < text.length; i++) {
if (for (var j=i; j<(i + myWord.length); j++){
text[j]===myWord[j-i]
})
hits.push(text[i])
}
}
if (hits.length === 0) {
console.log("Your name wasn't found!");
}
else {
console.log(hits);
}
You should move the if
condition inside the inner loop like below
for (var i = 0; i < text.length; i++) {
for (var j = i; j < (i + myWord.length); j++){
if(text[j]===myWord[j-i])
hits.push(text[i]);
}
}
}
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.