0

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

2
  • What is hits.length === [0] ? Do you mean hits.length === 0 ? Commented Aug 19, 2016 at 5:22
  • Try using an IDE to spot those syntax error. Jetbrain's Webstorm is pretty good for javascript programming. Commented Aug 19, 2016 at 5:23

3 Answers 3

1

try this

var jshint_multistr = true;

instead of

jshint multistr:true 
Sign up to request clarification or add additional context in comments.

Comments

0

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.

Comments

0

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.