对于循环条件问题

时间:2015-11-05 20:26:56

标签: javascript arrays for-loop

我正在努力找出我正在做的javascript学习课程的条件。

我没有得到正常的错误,因为我在课程中这样做,但我在课程中收到的错误是这个......

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

这些是说明:

First, you'll want to set your second loop's iterator to start at the first one, so it picks up where that one left off. If your first loop starts with

for(var i = 0; // rest of loop setup
your second should be something like

for(var j = i; // rest of loop setup
Second, think hard about when your loop should stop. Check the Hint if you get stuck!

Finally, in the body of your loop, have your program use the .push() method of hits. Just like strings and arrays have a .length method, arrays have a .push() method that adds the thing between parentheses to the end of the array. For example,

newArray = [];
newArray.push('hello');
newArray[0];   // equals 'hello'

这是我的代码

var text = "Hello, my name is Becky. What is your name?\
I repeat, my name is Becky. Can't you figure out that my\
name is Becky. Becky!!!!";
var myName = "Becky";
var hits = [];
for (i = 0; i < text.length; i++) {
    if (text[i] === 'B') {
        for (var j = i; i < myName.length; i++) {
            hits.push();
        }
    }
}

我知道这个问题存在于这一行:

for (var j = i; i < myName.length; i++) {

我无法弄清楚我需要如何构建它。

更新:

问题的最终答案:

/*jshint multistr:true */
var text = "Hello, my name is Becky. What is your name?\
I repeat, my name is Becky. Can't you figure out that my\
name is Becky. Becky!!!!";
var myName = "Becky";
var hits = [];
for (i = 0; i < text.length; i++) {
    if (text[i] === 'B') {
        for (var j = i; j < (i + myName.length); j++) {
            hits.push(myName);
        }
    }
}
if (hits === 0) {
    console.log("Your name wasn't found!");
} else {
    console.log(hits);
}

2 个答案:

答案 0 :(得分:1)

我不确定你想要达到的目标,

这可能有所帮助

var text = "Hello, my name is Becky. What is your name?\
I repeat, my name is Becky. Can't you figure out that my\
name is Becky. Becky!!!!";
var myName = "Becky";
var hits = [];
for (i = 0; i < text.length; i++) {
    if (text[i] == 'B') {
        var res = '';
        for (var j = i; j < i+myName.length; j++) {
            res = res+text[j];
        }
        if(res == myName)
        {
            hits.push(myName);
        }
    }
}
console.log(hits);

这是demo

答案 1 :(得分:1)

我会给你解决方案,因为你可以从直接的解决方案中学到更多东西,而不是从那个方向开始学习。

<key>NSAppTransportSecurity</key>
<dict>
    <key>NSExceptionDomains</key>
    <dict>
        <key>facebook.com</key>
        <dict>
            <key>NSIncludesSubdomains</key>
            <true/>                
            <key>NSThirdPartyExceptionRequiresForwardSecrecy</key>
            <false/>
        </dict>
        <key>fbcdn.net</key>
        <dict>
            <key>NSIncludesSubdomains</key>
            <true/>
            <key>NSThirdPartyExceptionRequiresForwardSecrecy</key>
            <false/>
        </dict>
        <key>akamaihd.net</key>
        <dict>
            <key>NSIncludesSubdomains</key>
            <true/>
            <key>NSThirdPartyExceptionRequiresForwardSecrecy</key>
            <false/>
        </dict>
    </dict>
</dict>

可能还有其他方法可以达到此结果。这是其中之一。

解释&#34;推送&#34;确实

数组是变量列表。您将值存储在如下变量中:

var text = "Hello, my name is Becky. What is your name?\
I repeat, my name is Becky. Can't you figure out that my\
name is Becky. Becky!!!!";
var myName = "Becky";
var hits = [];
for (i = 0; i < text.length; i++) {
    if (text[i] == 'B') {
        var equal = true;
        for (var j = 0; j < myName.length; j++) {
            if (text[i + j] != myName[j]) {
                equal = false;
                break;
            } 
        }
        if(equal) hits.push(myName);
    }
}

数组声明如下所示:

var myNumber = 777;
var myName = "Nelson";

然后你把它放进去,就像这样:

var myNumbers = [];

然后如果你尝试:myNumbers.push(333); myNumbers.push(555); myNumbers.push(777); ,它会打印:[333,555,777]

如果您添加另一个推送:

console.log(myNumbers)

会将999添加到列表中,从而生成myNumbers.push(999);

选中此demo

得到了吗?看看这里更详细的解释:

http://www.hunlock.com/blogs/Mastering_Javascript_Arrays