This forum is now read-only. Please use our new forums at discuss.codecademy.com.

407 points
559c14a3e39efee4b80004bd_405952360
Submitted by
Sharon
over 3 years ago

Please help me! I cant figure this out! myName.length

I am doing what the instructions say, but it wont pass me. What am I doing wrong? Some explanation would be appreciated. Below is my code.

/*jshint multistr:true
var text = "Hey! my name is Sharon! I love music \ and am awesome!";
var myName = "Sharon";
var hits = [];
for(var I = 0; I <text.length; I++){
     if(text[j]==="S"){
            for(var j = I; j <myName.length; j++){
            }
     }
}

Thank you for the help!


0 votes

Best answer

permalink

/*jshint multistr:true */

var text = "Hay! my name is Sharon! I love art. I also \
love music!";

var myName = "Sharon";

var hits = [];

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

}

for(var j = i; j < i; j ++){
hits.push(j)

}

}

I hope this helps. I seriously can't figure this out.

407 points
559c14a3e39efee4b80004bd_405952360
Submitted by
Sharon
over 3 years ago

3 Comments

5674c6dae39efec455000434_879409444 Zeke Y over 3 years ago

You're closing your if statement before the second for loop. The for loop needs to be inside the if statement. Then, change this line: hits.push(j) to this: hits.push(text[j]);

559c14a3e39efee4b80004bd_405952360 Sharon over 3 years ago

Thanks, I figured it out with a little help from Lelouch Lamperouge. Thanks foe the help though!

5674c6dae39efec455000434_879409444 Zeke Y over 3 years ago

No problem, glad Lelouch was able to help you get it sorted out :)


0 votes

permalink

Hi Sharon,

I think you need to be pushing text[j] to the hits array in your second for loop.

2835 points
5674c6dae39efec455000434_879409444
Submitted by
Zeke Y
over 3 years ago

7 Comments

559c14a3e39efee4b80004bd_405952360 Sharon over 3 years ago

Thanks,
I'm a little confused though. Would you mind explaining it a bit so I know what I did wrong?

5674c6dae39efec455000434_879409444 Zeke Y over 3 years ago

Um... you forgot to do anything in the second for loop...

5674c6dae39efec455000434_879409444 Zeke Y over 3 years ago

Oh wait, did you want to know what pushing text[j] to the hits array did?

5674c6dae39efec455000434_879409444 Zeke Y over 3 years ago

It just adds the jth character in text to hits. So, if text contains one word, "hello", and j equals 3, then in hits we would see "l". Does that help?

559c14a3e39efee4b80004bd_405952360 Sharon over 3 years ago

I don't know. I put 'j' in the 'hits' array, but I keep getting this

Oops, try again. Careful: your second 'for' loop should stop when it reaches its current point in the string + myName.length

5674c6dae39efec455000434_879409444 Zeke Y over 3 years ago

Could you please post your entire code? (Please in a reply, not a comment)

559c14a3e39efee4b80004bd_405952360 Sharon over 3 years ago

Yah no problem.


0 votes

permalink

/*jshint multistr:true */

text = "Lelouch Vi Britannia commands you! shineii!! Watashiwa Zero!!moshi moshi!!  Watashiwa Zero!! Watashiwa Zero!! Watashiwa Zero!!";

var myName = "Zero";
var hits = [];

// Look for "Z" in the text
for(var i = 0; i < text.length; i++) {
    if (text[i] === "Z") {
        // If we find it, add characters up to
        // the length of my name to the array
        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);
}
var game = function() {
    var qoutes = prompt("Before you proceed. Do you want to read a famous qoutes of mine?");
    switch(qoutes) {
    case "yes":
        alert("If the King doesn't move, how suppose will his ordinates follow");
        break;
    case "no":
        alert("You will regret it!! Lelouch Vi Britannia commands you, Shineii!!")
        break;
    default:
        alert("Die!!!!!!")
    }
};

game();

19 points
55bd255a937676e734000514_799258500
Submitted by
Lelouch Lamperouge
over 3 years ago

8 Comments

559c14a3e39efee4b80004bd_405952360 Sharon over 3 years ago

what?

55bd255a937676e734000514_799258500 Lelouch Lamperouge over 3 years ago

I just play a little bit xD

559c14a3e39efee4b80004bd_405952360 Sharon over 3 years ago

lol, can you help me though, please

55bd255a937676e734000514_799258500 Lelouch Lamperouge over 3 years ago

haha, ok. can you state what is your problem? I suggest Zeke Y has a better one

55bd255a937676e734000514_799258500 Lelouch Lamperouge over 3 years ago

Oh, I see. Here:) in your for loop copy and paste this

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

55bd255a937676e734000514_799258500 Lelouch Lamperouge over 3 years ago

just let me know if it still doesnt work:D

559c14a3e39efee4b80004bd_405952360 Sharon over 3 years ago

Thank you soooooooooooooo much! And FYI, the I++ is supposed to be a j++. Thank you!!!!!!!!!!!!XD

55bd255a937676e734000514_799258500 Lelouch Lamperouge over 3 years ago

oh xD, my bad.. hehe