Listening In

← Forum
267 pts

jonathan schilmoeller

[HELP] Tried to improve the script and hit a wall

So I tried to improve it and now, instead of the 3 times myName is in the text, it appears 4 times in the console...
Any help would be greatly appreciated, here's the code below:

/*jshint multistr:true */

var text = "tg tvyuhb cyvtu Jonathan ftgyuoih ftyguo crtyivg iy icy/ gvtiftyv ytvg tvygc fgi i cfgiv uicfgu figk ufgk Jonathan ci ygvt gvty/  tucgyiv ui Jonathan ciyhbc ivygc ihbjlc ficfi Juejue gcif ghlji gvh";
 var myName = "Jonathan";
tempHits = []; 
hits = [];
  for (var i = 0; i < text.length; i++ ){
  if (text[i] === myName[0]){          
      for (var k = i; k < i + myName.length; k++){
          tempHits.push(text[k]);
          var l = k-i;
          if (tempHits[l] === myName[l]){
              hits.push(tempHits[l]);
          }
      }
  }
}

  if (hits.length === 0){
  console.log("your name wasn't found");
} else {
  console.log(hits);
}
  • 800 pts

    Angus Bayley

    1 votes

    Ok, I figured it out. Here's how to understand what's going on:

    • try using console.log(tempHits); at the bottom of your code to see what tempHits actually contains by the time the script has been executed.

    Also realise the following:

    • your variable l will only ever have a value between 0 and 8 (myName.length = 8)
    • hits is populated by hits.push(tempHits[l]), so if l can only be between 0 and 8, then only the first 8 letters of tempHits can ever be pushed to hits.
    • Jonathan appears in text three times, but there is also a fourth word that starts with a capital J - Juejue. It's the capital J that triggers the second for loop (line 9), so you get the first 8 letters of tempHits pushed to hits 4 times.

    Make sense?

    • jonathan schilmoeller over 1 year ago
      Hey, I thought about what you said, and you are right, l would stay the same, so I added this line to the function, now it works :D if (tempHits.length === myName.length){ tempHits.length = 0;
  • 267 pts

    jonathan schilmoeller

    1 votes

    It works now, and looks like this :) With the change I empty the array everytime after myName is pushed into hits avoiding different values for l
    /*jshint multistr:true */

    var text = "tg tvyuhb cyvtu Jonathan ftgyuoih ftyguo crtyivg iy icy/ gvtiftyv ytvg tvygc fgi i cfgiv uicfgu figk ufgk Jonathan ci ygvt gvty/ tucgyiv ui Jonathan ciyhbc ivygc ihblc ficfi ueue gcif ghli gvh";
    var myName = "Jonathan";
    tempHits = [];
    hits = [];
    for (var i = 0; i < text.length; i++ ){
    if (text[i] === myName[0]){

    for (var k = i; k < i + myName.length; k++){
    tempHits.push(text[k]);
    var l = k-i;
    if (tempHits[l] === myName[l]){
    hits.push(tempHits[l]);
    if (tempHits.length === myName.length){
    tempHits.length = 0;
    }

    }
    }
    }
    }

    if (hits.length === 0){
    console.log("your name wasn't found");
    } else {
    console.log(hits);
    }

Sign in to post to the forum.

Sign in