Skip to content Skip to sidebar Skip to footer

Simple Beginner Search Program Using Arrays In Javascript, Issue With Displaying

I am creating a simple search program that searches for my name block of text. The issue I am having is at the end, when I place the letters in the array, they seem to come out a s

Solution 1:

The reason of "letter by letter" behaviour is that variable hits contains an array of single letters from all the matches.

You either need to call join method on array with letters from one name found, or use something more advanced instead of cycles. For example, text.match(/Aaron/g) will return an array of all names matched.


Solution 2:

I think this search program is much more valid:

var text = "Blah blah blah blah blah blah Eric \
blah blah blah Eric blah blah Eric blah blah \
blah blah blah blah blah Eri Epic Erics☺n"
var name = "Eric"
var x=0;
var y=0

for (var i = 0; i < text.length; i++) {
  x=0;
  for (var j = i; j < name.length + i; j++) {
    if (text[j] === name[j-i]) {
      x++;
      if(x === name.length) {
        y++
      }
    }
  }
}
console.log("Found "+y+" times")

Post a Comment for "Simple Beginner Search Program Using Arrays In Javascript, Issue With Displaying"