# It keeps crashing, what's wrong here?

#1

/*jshint multistr:true */

var text = "Marvin is cool and good and Marvin is awesome, \
he's really cool and fun too."

var myName = "Marvin"

var hits = [];

for(var i = 0; i < text.length;i++) {
if(text[i] === "M") {
for(var j = i; i + myName.length; i = j++) {
hits.push(text[j]);
}
}
}

#2

I think you have an infinite loop here:

``for(var j = i; i + myName.length; i = j++)``

A for loop takes three pieces of information. The first is where to start the loop; the second, how long to continue the loop; and the third, how to adjust between loops.

So when you say `for(var i = 0; i < text.length;i++)` you're telling the computer to start with the variable i at 0, looping as long as i is still less than text.length, and increasing i by one for each new loop. But when you say `for(var j = i; i + myName.length; i = j++)` you're telling the computer to start with the variable j equal to... an undefined i, looping as long as (undefined plus a number), and with each new loop set i equal to j+1. So if this loop works at all, there's nothing to stop it from looping forever... which will crash the browser.

#3

This same program keeps crashing my browser too... I am no simply able figure out the problem... Here's is the code I entered.....
/*jshint multistr:true */
var text = "Ritam is taking a bag of Ritam to Ritam house with\ his friend Ritam";
var myName = "Ritam";
var hits = [];
for (var i = 0; i < text.length; i++)
{
if (text[i] === "R")
{
for(var x = i; x <= myName.length; i++)
{
hits.push(x);
}
}
}

#4

var text = "Marvin is cool and good and Marvin is awesome, \
he's really cool and fun too."; Missing a semi-colon

var myName = "Marvin"; Missing a semi-colon

var hits = [];

for(var i = 0; i < text.length; i++) {
if(text[i] === "M") {
for(var j = i; j < i + myName.length; j++) { j has to be less than text.length + i or it'll crash. Also, you're adding to just j (not i)
hits.push(text[j]);
}
}
}

// this actually prints it out
if (hits === 0) {
}
else {
console.log(hits);
}

#5

try this:

var text = "Ritam is taking a bag of Ritam to Ritam house with his friend Ritam";
var myName = "Ritam";
var hits = [];
for (var i = 0; i < text.length; i++) {
if (text[i] === "R") {
for(var x = i; x < myName.length + i you have to start at i; x++ you're adding to x and not i)
eliminate {
hits.push(x); should be in the same code section
Delete the unnecessary }
}
}

this actually prints it out
if (hits === 0) {
}
else {
console.log(hits);
}

#6

thanx.... but I would like to ask something I have been confused with... When I submit the code... it says that I am I am unable to push letters into the array, instead I am pushing in numbers.... Is there a bug in it...?
/*jshint multistr:true */
var text = "Ritam is taking a bag of Ritam to Ritam house with\ his friend Ritam";
var myName = "Ritam";
var hits = [];
for (var i = 0; i < text.length; i++)
{
if (text[i] === "R")
{
for(var x = i; x < myName.length + i; x++)
{
hits.push(x);
}
}
}