This forum is now read-only. Please use our new forums! Go to forums
[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);
}
Answer 516b2b4c3652fc08f300246f
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 byhits.push(tempHits[l])
, so ifl
can only be between 0 and 8, then only the first 8 letters oftempHits
can ever be pushed tohits
.- 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 secondfor
loop (line 9), so you get the first 8 letters oftempHits
pushed tohits
4 times.
Make sense?
Answer 516c32e31cc98ebdaa000488
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); }
Popular free courses
- Free Course
Learn SQL
In this SQL course, you'll learn how to manage large datasets and analyze real data using the standard data management language.Beginner friendly,4 LessonsLanguage Fluency - Free Course
Learn JavaScript
Learn how to use JavaScript — a powerful and flexible programming language for adding website interactivity.Beginner friendly,11 LessonsLanguage Fluency - Free Course
Learn HTML
Start at the beginning by learning HTML basics — an important foundation for building and editing web pages.Beginner friendly,6 LessonsLanguage Fluency
1 comments
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;