I was trying to create a search in JS to find a given name in the body of a text. However, the search returns no value at all. The logic I used is:
<script>
var text = "Xxxxx, xxxx x xxxx x xxxx xxxxxx xxxxxxx. Lucas Menezes";
var myName = "Lucas";
var hits = [];
for (var x = 0; x < text.length; x = x + 1) {
var adiction = myName.length + x;
if (text[x] === "L") {
if (text[adiction] === "s") {
for (var i = x; i < adiction; i = i + 1) {
hits.push(text[i]);
};
};
};
};
if (hits.length === 0) {
console.log("Your name wasn't found");
console.log(adiction);
} else {
console.log(hits);
console.log(adiction);
}
</script>
Can anyone help me by saying what the problem is? The array hits returns empty. If I remove the second IF
it even works, however it returns an inaccurate search showing any name that starts with L
and its 4 subsequent characters.