6
I was trying to create a JS search to find a given name in the body of a text. However, the search does not return any value. 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>
Could someone help me by telling me what the problem is? The array hits empty turn. If remove the second IF
it even works, however returns me an inaccurate search presenting any name that begins with L
and the 4 subsequent characters.
What result do you expect?
– JuniorNunes
I was hoping the hit array would bring me: [L,u,c,s]
– Lucas Menezes
rsrsrs sorry. I’m new around here
– Lucas Menezes
I get it, you need to do it this way, or another way would do it too?
– JuniorNunes
Actually brother, it is only a basic exercise, I am new in programming. If it is possible that way, I thank you. But if you find another convenient way for a beginner. I’m already very grateful.
– Lucas Menezes