View Single Post
Old 12-18-2012, 04:13 PM   PM User | #1
Entity_
New Coder

 
Join Date: Dec 2012
Location: Ingerlund
Posts: 62
Thanks: 8
Thanked 4 Times in 4 Posts
Entity_ is an unknown quantity at this point
Moving text to an array - question. Easy for you master coders!

Code:
text = "Blah blah blah blah blah blah Tim \
blah blah blah Tim blah blah Tim blah blah \
blah blah blah blah blah Tim";

var myName = "Tim";
var hits = [];

for (var i = 0; i < text.length; i++){
	if (text[i]== "T"){
		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);
}
Hi! This is basically a small project to search the text above and to move the letters of a name to an array using for loops. I've constructed the code in a specific way pertinent to the exercise I was doing on CodeAcademy, but I just don't understand this part - (myName.length + i). I don't know why I have to put + i there, why can't I just tell the code to stop running when the letters that have been moved to the array are EQUAL to myname? I hope that makes sense.
Entity_ is offline   Reply With Quote