This forum is now read-only. Please use our new forums! Go to forums

0 points
Submitted by Sharon
over 8 years

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!

Answer 55bf974393767660f0000002

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.

points
Submitted by Sharon
over 8 years

3 comments

Zeke Y over 8 years

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

Sharon over 8 years

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

Zeke Y over 8 years

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

Answer 55bacf4f937676797100006f

0 votes

Permalink

Hi Sharon,

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

points
Submitted by Zeke Y
over 8 years

7 comments

Sharon over 8 years

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

Zeke Y over 8 years

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

Zeke Y over 8 years

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

Zeke Y over 8 years

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?

Sharon over 8 years

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

Zeke Y over 8 years

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

Sharon over 8 years

Yah no problem.

Answer 55bc9ff2e39efefadb000082

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();
points
Submitted by Lelouch Lamperouge
over 8 years

8 comments

Sharon over 8 years

what?

Lelouch Lamperouge over 8 years

I just play a little bit xD

Sharon over 8 years

lol, can you help me though, please

Lelouch Lamperouge over 8 years

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

Lelouch Lamperouge over 8 years

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

Lelouch Lamperouge over 8 years

just let me know if it still doesnt work:D

Sharon over 8 years

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

Lelouch Lamperouge over 8 years

oh xD, my bad.. hehe