我目前在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