Searching Text for your name 5: Your second 'if' loop & 6: Log it


#1


Hello,
I just wrapped up the section on loops and though I managed to patch together the code that allowed me to move forward, I'm still not 100% on all the parts and would love to articulate my understanding and if anyone would be good enough to validate my correct thinking and correct my invalid thinking - i would be very appreciative! it'll help me when i move forward to 'while' loops! Thanks!

var text = "Blah Julie giddidy tuesday raining Julie.";
var myName = "Julie";
var hits = [];
	for(var i = 0; i < text.length; i++) {
	if (text[i] === "J") {

	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);
}

var text = "Blah Julie giddidy tuesday raining Julie.";
(obviously, this is a variable declaring a string of text that will be searched)

var myName = "Julie";
(also, a variable declaring that name that will be search from the above text)

var hits = [];
(I am assuming - i maybe be wrong! - it wasn't explained - that this is an array 'container' that will be filled with the 'hits' - the results of the search - kinda like the bucket your carry to put the apples that you're gonna pick! lol)

for(var i = 0; i < text.length; i++) {

(the 'for' loop declaring where the a) the search ought to start - not sure if this refers to zero index POSITION in the text or zero VALUE - though it can't be a 'value' because its text....I'm assuming its POSITION ie at the beginning of the text.....2) ends....now is where I'm uncertain!...is the text.length referring to the length of the line of text declared above? If so, why? This bit is well dodgy! , and finally c) it will increase by increment of 1)

if (text[i] === "J") {
(So, this is the condition, its saying that if the text it searches is a 'J', then ...)

for(var j = i; j < (myName.length + i); j++) {
		hits.push(text[j]);

(why is this a 'j'? this was provided by Code Academy. At first I though coz i was looking for a J of my first name that this was related...but not so. I think that it has something to do with 'continuing the previous' loop - kind of going on from where loop 1 left off - but why j? I noticed the order is inverted. If anyone could explain this bit to where I understand it, it would mean I could treat myself to creme brulee tonight as a reward...otherwise i'm only allowed gruel!:wink:)
}
}
}
if (hits.length === 0) {
console.log("Your name wasn't found!");
} else {
console.log(hits);
(this bit is concluding that if the length of the result is zero ie it wasn't a J and therefore, wasn't a hit, then it will print the stated message, otherwise it will put the results in the 'hits' bucket! and print that bucket o' stuff)
}

Phew! Sorry for the long bloody post! :wink:


#2

yes, it is an array. You are going to push/insert the result into this array.

this is simply a zero value, but we are going to make the values in such a way that they exactly match the indexes of the string stored in text

text is a variable holding a string. so this will give you the length of the string stored in the text variable (which you declared earlier), then we increase by one to get to the next letter, until we finished looping over the entire string in text

this condition will search for the first letter of your name, once your first letter is found we are going to loop over the rest of your name. you could also do:

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

Does that make sense? first letter of your name, but strings are zero index based

because it can be? this really could be named anything we like. Why the creator of the course picked j? i guess we will never know.

just like we choice i for our first loop:

for(var i = 0;

why did we pick i? could also have been any name we like.

back to your second for loop:

for(var j = i;

instead of assigning a number directly to j, we give it a number which is stored in a variable. We do this because we might need need this loop multiply times, in case your name is multiply times in text. i contains the index of the first letter of your name thanks to your if condition.


#3

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.