user6716620
user6716620

Reputation:

SyntaxError: Unexpected Identifier for loop through an array

Writing a loop and array that searches text for a word and it comes up with this syntax error I've been looking over the code but can't seem to notice any errors.

jshint multistr:true 

var text = "Usually, solar companies install\
solar panels on roofs but Elon Musk offers an\
entirely different and ingenious approach";

var myName = "Elon";

var hits = [];

for(var i=0; i <= text.length; i++) {
   if(text[i] === 'E') {
       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);
}

Upvotes: 0

Views: 438

Answers (3)

Shambhu Kumar
Shambhu Kumar

Reputation: 26

You are using jshint directives in wrong way, you should put it inside /* */ tag

Try the code below..
it will work.

/* jshint multistr:true */

var text = "Usually, solar companies install\
solar panels on roofs but Elon Musk offers an\
entirely different and ingenious approach";

var myName = "Elon";

var hits = [];

for(var i=0; i <= text.length; i++) {
   if(text[i] === 'E') {
       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);
}

Upvotes: 0

user6716620
user6716620

Reputation:

Ok never mind turns out it was the "jshint multistr:true" at the top. No idea what it does or why it causes an error but codeacademy had it there as a comment which I unsilenced and forgot about.

Upvotes: 0

Akshay
Akshay

Reputation: 805

try this

var jshint_multistr = true;

instead of

jshint multistr:true 

Upvotes: 1

Related Questions