简体   繁体   English

Codecademy JavaScript Push()练习

[英]Codecademy JavaScript Push() Exercise

The goal is unclear on CodeAcademy. 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. http://bit.ly/167N8bX我想我应该通过一个长字符串,并将我的名字中的字符从字符串中推入一个数组。

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 我在7的第5步:链接: 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? * UPDATED如何保持每个循环的输出,并将每个推送存储在数组中,直到它完成所有循环?

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. 您的代码没有任何问题,除非您将text[i]而不是text[i+j]推送到hits数组中。

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 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"). 教师的代码并没有真正检查我的名字的字符串(“Ryan”)。 His code doesn't use the first letter of the MyName array. 他的代码不使用MyName数组的第一个字母。 He manually inputs "E" ("Eric" is his example). 他手动输入“E”(“Eric”是他的例子)。 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. 他的代码发现手动输入“E”,然后在“E”之后推送接下来的3个字符(他们不必使用myName.length将“ric”匹配到hits []数组中。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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