我目前在Code Academy上玩得很开心学习。他们推出了推。并且在没有大量初始信息的情况下很快嵌套循环。我想我有点理解其中的逻辑,并想看看是否有人能帮助打破它......请帮我理解这个'for循环'中的逻辑
var text = "Here is a string with the name rick sometimes not always rick and sometimes rick";
//assigning text string to variable text
var myName = "rick";
//assigning rick to variable myName
var hits = [];
// assinging an empty array to variable hits
for (var i = 0; i < text.length; i++); {
//for loop i = 0, and as long as i = less than the length of entire text string keep incrementing 1
if (text[i] === "r") {
//while looping through text when you find an "r" enter second for loop
for(var j = i; j < (i+ myName.length); j++){
//J takes the value of i at this point and it should be 0 and should increment 4 steps as myName = 4 characters
hits.push(text[j]);
//this push statement should add each letter of my name to the hits array
}
}
}
这时我的代码不能正常工作。我在第一个for循环中放置了一个console.log,它只是打印出84. “console.log(”I =“+ I)” 我明白这很漂亮n00b,但我真的想遵循逻辑,理解什么是发生。我关门了吗?
很难读取没有缩进的代码。如果您可以轻松阅读,更多的人会关注您的问题。 – jfriend00
“// J在这一点上取我的值,它应该是0”这个评论是不正确的。 'j'将等于'i',它可能小于'text.length'。对于第一次发现的文本,它会发现'r''i'将等于2,因为它会在“Here”中看到'r'。 – DigitalNinja
那么究竟是你期望的结果?什么是问题?在第一眼看来,内循环对我来说毫无意义。 – dreamlab