I pass I get 630 not Velma - where have I gone wrong?


#1

WHY DOES IT COUNT THE ENTIRE text array? What am I missing?

Search Text for Your Name
Second loop instructions are:
Okay! Go ahead and add that second for loop inside the body of your if statement.
My answer is 630 and I pass the tutorial it says move to the next lesson.

text = "four score and seven years ago, Velma, our nation,\ founded on the principal, that all men, Velma, and women,\are created equal.";
var myName = "Velma";
 var hits = []
 for (var i = 0; i< text.length; i++){
     if (text[i] === "V");
     for(var j = i; j < (myName.length + i); j++) {
            hits.push(text[j]);
        }
    }

Thanks in advance for your help. Please explain. I've nearly lost my mind with Syntax, now that I don't get those I still can't get the code to produce the desired result.


#2

There should be an open curly brace here instead of a semicolon. You should try running your code on https://repl.it and see what errors pop up.


#3

Thanks. When I pull the ; I get the syntax error
and on https://repl.it all I get when I run the code is the 630 characters of text in the array.
No errors no colors. No name -no count either.
So confused.


#4

You're not getting a name because you don't have anything logging the hits array to the console yet. Also, when you add the { after the if condition, remember to add a closing } brace at the end of your code.


#5

That worked -sorta. Now I get 10 - so I'm still just counting and not searching the array for myName

Can you show me please? What it is I am supposed to do the add something to hit the array?
Here's my code with your correction and an attempt to hit the array
/*jshint multistr:true */
text = "four score and seven years ago, Velma, our nation,\ founded on the principal, that all men, Velma, and women,\are created equal.";
var myName = "Velma";
var hits = []
for (var i = 0; i< text.length; i++){
if (text[i] === myName[0]){
for(var j = i; j < (myName.length + i); j++) {
hits.push(text[j]);
}
}
}


#6

This is taken directly from the instructions:

Under your existing code (and outside all your loops!), set up an if/else statement.
If you don't have any hits, log "Your name wasn't found!" to the console. Otherwise, log the hits array to the console.

Then the hint:

Feel free to peek back at the first exercise if you need help! Your console.log() code should look something like this:

if(/*hits.length is 0*/) {
console.log("Your name wasn't found!");
} else {
console.log(hits);
}


#7

Thanks for your help with the syntax it worked and the code passed lesson 5 and while I wasn't expecting 10 as a result I moved on.
I was asking about lesson 5 search for your name not lesson 6 log it.
The instructions should point out in lesson 5 that you will still just be counting. It would be far less confusing for a beginner.
And yes lesson 6 worked because you helped me get lesson 5 to run.


#8

I thought you were on lesson 6. Let's go back to your original code for a moment.


#9

Okay now that makes sense. Thanks for coming back to explain what was going on.


#10

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