简体   繁体   中英

Codecademy JavaScript Push() Exercise

The goal is unclear on CodeAcademy. http://bit.ly/167N8bX I think am supposed to run through a long string, and push the characters of my name out of the string into an array.

Here's how it's stated:

"it will check the text for the first letter of your name, then push (add) the number of characters equal to your name's length to an array. By inspecting the array, you'll be able to see if your name was mentioned!"

*UPDATED. Now that I see the instructor's output, it doesn't actually check to see if your name is mentioned at all. Confusing instructions for a novice, like me.

I am on step 5 of 7: Link: http://bit.ly/167N8bX

var text = "Lorem ipsum dolor nayr sit amet, consectetur adipisicing elit, sed do eiusmod tempor yan ut Ryan labore et dolore magna aliqua. Ut enim ad ry minim veniam, quis nostrud ryan exercitation ullamco ryan laboris nisi ut aliquip ex ea ry commodo rya consequat. END";
var myName = "Ryan";
var hits = [];

for(var i = 0; i < text.length; i++) {
    // Loop thru "text" string
    // check each char one-by-one
    // if it finds uppercase "R"
    if(text[i] == "r".toUpperCase()){

    // push the next 3 chars into hits[] array...
    // by looping on myName.length
    // end push() when myName.length loops ends
       for(var j = 0; j < myName.length; j++){
           hits.push(text[i]);
           console.log(hits);
       }
    }
}

*UPDATED How do you keep the output from occurring each loop, and store each push in the array, until after it finishes all the looping?

My incorrect output from the above code:

[ 'R' ]
[ 'R', 'y' ]
[ 'R', 'y', 'a' ]
[ 'R', 'y', 'a', 'n' ]

I think the issue is that in your inner loop you are only pushing the same letter every time. You need to modify it to the following:

hit.push(text[i + j])

That should fix the problem. Good luck with the continued coding!

Just saw the exercise of code academy. There is nothing wrong with your code except, you are pushing text[i] instead of text[i+j] into hits array.

Actually code academy wanted you will write the code according to their hints.

In their hint you will be able to see Your loop should stop when it hits the value of the first iterator (say, i) plus the length of your myName variable.

your code

var text = "Lorem ipsum dolor nayr sit amet, consectetur adipisicing elit, sed do eiusmod tempor yan ut Ryan labore et dolore magna aliqua. Ut enim ad ry minim veniam, quis nostrud ryan exercitation ullamco ryan laboris nisi ut aliquip ex ea ry commodo rya consequat. END";
var myName = "Ryan";
var hits = []; // empty array to 'push()' my name into
for(var i = 0; i < text.length; i++ ){
    if(text[i] == myName[0]){ 
        for(var j = 0; j < myName.length; j++){ 
            hits.push(text[j+i]);
        }
    }        
}

If you replace you code with the below code, they will say okay

var text = "Lorem ipsum dolor nayr sit amet, consectetur adipisicing elit, sed do eiusmod tempor yan ut Ryan labore et dolore magna aliqua. Ut enim ad ry minim veniam, quis nostrud ryan exercitation ullamco ryan laboris nisi ut aliquip ex ea ry commodo rya consequat. END";
var myName = "Ryan";
var hits = []; // empty array to 'push()' my name into
for(var i = 0; i < text.length; i++ ){
    if(text[i] == myName[0]){ 
        for(var j = -1; j < myName.length+1; j++){ 
            hits.push(text[j+i+1]);
        }
    }        
}

I get it now. The instructor's code doesn't really check my name's string ("Ryan"). His code doesn't use the first letter of the MyName array. He manually inputs "E" ("Eric" is his example). His code finds that manually inputed "E", and then pushes the next 3 characters after the "E" (they do NOT have to match "ric" into the hits[] array, using myName.length.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM