4. What Am I Missing? SOLVED


#1

/*jshint multistr:true */
var text = "My name is Christopher. Hardy har har.";
var myName = "Christopher";
var hits = [];
for(var i=0; i < text.length; i++){(if (text[i] === 'C')
}
}

The text above is the code that I put in. I can't seem to find a problem in the code, but it's telling me that I can't proceed to the next lesson. Am I missing soemthing? PLEASE HELP!!!!


#2

Remove all curly brackets and use semi-colon.

for (var i=0; i < text.length; i++); 
if (text[i] === "C");

#3

Your solution is incomplete. Missing inner loop and you should really type the array as string var hits = [""]; is a type shortcut to data type string '' for char. Checkout my solution:

`//jshint multistr:true;
var text = "I love life and John John Groovy man!";
var myName = "John";
var hits = [""]; //must be letters (chars) so "" types array as string

for(var i = 0; i < text.length; i++) {
if(text[i] === myName[0]) {
for(var j = i; j < myName.length + i; j++) {
hits.push([j]);
}
}
}
if(hits.length === 0) {
console.log("Name not found...");
}
else {
console.log("Result is "+hits.length);
}
`


#4

@bayoishola20 thanks for your suggestion, but it didn't work. Maybe I'm doing it wrong? If you did this lesson can you send me your code so I can look at it?


#5

@johmmlhll Thanks for your suggestion, but it didn't work. Maybe I'm doing it wrong? If you did this lesson can you send me your code so I can look at it? I always get confused with the semi colons and other things.


#6

Your for & if part has an incorrect structure.
Sample structure:
for ( ) {
if ( ) { }
}


Be careful with the brackets and it should work:

for (var i = 0; i <= text.length; i++){
if (text[i] === "C") {
}
}


#7

var text=("banana","henry","banana","henry","banana","henry","banana");
var myName="henry";
var hits=[];
for (var i=0;
i===text.length;
i++);
if(text[i]==='h');

this is what i did..


#8

`//jshint multistr:true;
var text = "I love life and John John Groovy man!";
var myName = "John";
var hits = [""]; //must be letters (chars) so "" types array as string

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

the above is correct. I strongly recommend you get familiar with syntax first bro! It has a purpose. For example in Javascript like many other languages, semicolon tells the interpreter when to stop reading in the code and () acts programatically with function to take in arguments and without to priortise number calculations. Javascript is a dynamic, high level interpretive language. Some more on it... http://programmers.stackexchange.com/questions/138521/is-javascript-interpreted-by-design


#9

Sorry you didn't get it. Here's the full code suggested:

var text = "My name is Christopher. Hardy har har";
var myName = "Christopher";
var hits = [];
for (var i=0; i < text.length; i++); 
if (text[i] === "S");

#10

Thank you for ALL your guys's help! @bayoishola20 @johmmlhll @kchii @datapro43876 @kchii


#11

You're welcome.

Have fun coding! :slight_smile:


#12

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