Your second "for" loop


#1

i have a problem:

var text = " fjFJeopf ESFehfOIEFH fbSFEUfheo fndq dqwdg mirco dwhAAFHefgefgEFGugf";
var myName ="mirco";
var hits=[];
for ( var i=0; i <= text.length; i++) {
if (text[i] === "m"){
console.log(text[i]);
for ( var j = i; j < (myName.length + i); j++){
hits.push(text[j]);
}
}
}
the console prints:
m
20
and i can go to the next step..but it dosnt work like it schould :frowning:


#2

Why doesn't it work as it should?


#3

is it normal that the console prints:
m
20?
why 20?


#4

well, the m is logic, you asked for the m to be logged:

console.log(text[i]);

it should give 5, the length of hits array. Seems your browser is sending cached data

all good, move on, where you go about logging your name, the output then should be your name


#5

yes ok :slight_smile:
but now this ■■■■ m come out one more time here:
var text = " fjFJeopf ESFehfOIEFH fbSFEUfheo fndq dqwdg mirco dwhAAFHefgefgEFGugf";
var myName ="mirco";
var hits=[];
for ( var i=0; i < text.length; i++) {
if (text[i] === "m"){
console.log(text[i]);
for ( var j = i; j < (myName.length + i); j++){
hits.push(text[j]);
}
}
}
if (hits.length === 0) {
console.log("Your name wasn't found!");
}
else {
console.log(hits);
}

console prints:
m

[ 'm', 'i', 'r', 'c', 'o' ]

is it one more time the cached data? :confused:


#6

the m is coming from this console.log:

if (text[i] === "m"){
console.log(text[i]); // this one

#7

thanks.. i remake all this part..it is not all clear on my head :slight_smile:


#8

var text = " fjFJeopf ESFehfOIEFH fbSFEUfheo fndq dqwdg mirco dwhAAFHefgefgEFGugf";
var myName ="marco";
var hits=[];
for ( var i=0; i < text.length; i++) {
if (text[i] === "m"){
for ( var j = i; j < (myName.length + i); j++){
hits.push(text[j]);
}
}
}
if (hits.length === 0) {
console.log("Your name wasn't found!");
}
else {
console.log(hits);
}

if myName = "marco"
console prints:
[ 'm', 'i', 'r', 'c', 'o' ]
and not:
"Your name wasn't found!"


#9

this program is pretty limited, once the first letter of your name is a match, it assumes the name as a whole is a match

But its a great challenge to improve the program so it works for only your name, and not names starting with the same letter


#10

var text = " fjFJeopf ESFehfOIEFH fbSFEUfheo fndq dqwdg mirco dwhAAFHefgefgEFGugf";
var myName ="sebastian";
var hits=[];
for ( var i=0; i < text.length; i++) {
if (text[i] === "m"){
for ( var j = i; j < (myName.length + i); j++){
hits.push(text[j]);
}
}
}
if (hits.length === 0) {
console.log("Your name wasn't found!");
}
else {
console.log(hits);
}

console prints:
[ 'm', 'i', 'r', 'c', 'o', ' ', 'd', 'w', 'h' ]
i cant understand this :frowning:


#11

you still check if the first letter is m:

if (text[i] === "m")

a simply solution would be to do:

if (text[i] === myName[0])

this way, the first letter you look for automaticly changes when you change your name


#12

yeeeeeees..ok now i undestand..the console printed the length of the name Sebastian,but with the letter m right? ok ok..thanks for the help and sorry for the stupid questions :stuck_out_tongue:


#13

yes, exactly :slight_smile:

This is difficult part, good you understand it now :slight_smile:


#14