I signed up for Codecademy and I am currently in the Javascript path. So, there is this lesson called "Search Text for Your Name" and it consists of writing a multi line string and including your name on it and then write a loop that finds your name and logs it to the console. This is the code:
/*jshint multistr:true */
var text = "Blah Blah Pedro Blah Blah Blah Blah Pedro \
Blah Blah Blah Blah Pedro Blah Blah";
var myName = "Pedro";
var hits = [];
for (var i=0; i < text.length; i++){
if (text[i] ==="P"){
for (var j = i; j <(i + myName.length); j++){
hits.push(text[j]);
}
}
}
if (hits.length===0){
console.log("Your name wasn't found!");
}
else {
console.log(hits);
}
The part that confuses me is the second for loop. When I read "A Byte of Python",it said that when a variable = anothervariable, the variables point to the same place in the memory of the computer. But in the JavaScript code, if I change it, like instead of using hits.push(text[j]) I use hits.push(text[i]) or just switch them in any part of the loop the result is always different. Why is that so? How does Javascript treat this kind of variables?
variable = anothervariable" If that was written there, it is (most likely) wrong. Each variable is usually stored in its own memory location. If you havex = y, then the value ofyis copied to the location ofx.