2015-11-05 218 views
0

我有麻烦搞清楚我的JavaScript学习课程的条件。For循环条件问题

我没有得到正常的错误,因为我在使用过程中这样做,但我在使用过程中接收到错误是这样的......

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++) {

我只是无法弄清楚到底我需要构造它。

UPDATE:问题

最终的答案:

/*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); 
} 
+0

的关键是在评论这一部分:“在字符串中... **当前点** + myName.length ......” –

+0

那么'var j'?喜欢这个 '; j Becky

+0

首先分析一个for循环的结构:你有初始化,结束条件和增量。确定你的增量影响结束条件。 – dsh

回答

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 equal = true; 
     for (var j = 0; j < myName.length; j++) { 
      if (text[i + j] != myName[j]) { 
       equal = false; 
       break; 
      } 
     } 
     if(equal) hits.push(myName); 
    } 
} 

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

Explaing什么是 “推” 的作用:

数组是变量列表。你在一个变量中存储的值是这样的:

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

数组声明如下所示:

var myNumbers = []; 

然后你把东西在它的内部,像这样:

myNumbers.push(333); 
myNumbers.push(555); 
myNumbers.push(777); 

然后如果您尝试:console.log(myNumbers),它将打印:[333,555,777]

如果您添加ano疗法推:

myNumbers.push(999); 

将增加999导致[333, 555, 777, 999]

入住这demo

得到了它的名单?看看这里更详细的解释:

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

+0

它使我使用推式方法。 – Becky

+0

但它正在使用它。 –

+1

我仍然有点困惑,但我会再次上课。谢谢! – Becky

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