第6章
私は英語が下手で問題を詳しく説明できないので、練習コードを貼り付けます: http://jsbin.com/exusox/3/edit
ボタンをクリックすると無限ループします。
コードをテストしたところ、このコードで無限ループが見つかりました:
function extractFootnotes(paragraphs){
var footnotes=[],currentNum=0;
function dealWithFootnote(fragment){
if(fragment.type=="footnote"){
currentNum++;
fragment.number=currentNum;
footnotes.push(fragment);
return {type:"reference",number:currentNum};
}
else{
return fragment;
}
}
//run here ,endless loop happened.I've tested forEach,the i++ can't work.
forEach(paragraphs,function(paragraph){
paragraph.content=map(dealWithFootnote,paragraph.content);
});
return footnotes;
}
function forEach(array,action){
for(i=0;i<array.length;i++)
action(array[i]);
}
function map(func,array){
var result=[];
forEach(array,function(element){
result.push(func(element));
});
return result;
}
なぜこれが起こるのか、どうすればコードを減らすことができますか?ありがとう。