2012-10-06 78 views
1

我試圖在JavaScript中創建一個實踐的功能,並且下面的代碼似乎沒有正確執行或按照瀏覽器中的意圖執行。瀏覽器只是一直顯示忙標誌和計算機字面上hangs.Can任何人幫我出thius代碼或什麼的問題是:電腦掛在瀏覽器上運行此JavaScript代碼

function recluse() { 
    return " It is\ncalled Data.\n\nData is made of merely bits, yet it takes complex forms. Control\nconsists only of simple instructions, yet it performs difficult\ntasks. From the small and trivial, the large and complex arise.\n\nThe program source is Data. Control arises from it. The Control\nproceeds to create new Data. The one is born from the other, the\nother is useless without the one. This is the harmonious cycle of\nData and Control.\n\nOf themselves, Data and Control are without structure. The programmers\nof old moulded their programs out of this raw substance. Over time,\nthe amorphous Data has crystallised into data types, and the chaotic\nControl was restricted into control structures and functions.\n\n%% Short Sayings\n\nWhen a student asked Fu-Tzu about the nature of the cycle of Data and\nControl, Fu-Tzu replied 'Think of a compiler, compiling itself.'\n\nA student asked 'The programmers of old used only simple machines and\nno programming languages, yet they made beautiful programs. Why do we\nuse complicated machines and programming languages?'. Fu-Tzu replied\n'The builders of old used only sticks and clay, yet they made\nbeautiful huts.'\n\nA hermit spent ten years writing a program. 'My program can compute\nthe motion of the stars on a 286-computer running MS DOS', he proudly\nannounced. 'Nobody own a 286-computer or uses MS DOS anymore.', Fu-Tzu\nresponded.\n\nFu-Tzu had written a small program that was full of global state and\ndubious shortcuts. Reading it, a student asked 'You warned us against\nthese techniques, yet I find them in your program. How can this be?'\nFu-Tzu said 'There is no need to fetch water hose when the house is\nnot on fire.'{This is not to be read as an encouragement of sloppy\nprogramming, but rather as a warning against neurotic adherence to\nrules of thumb.}\n\n%% Wisdom\n\nA student was complaining about digital numbers. 'When I take the root\nof two and then square it again, the result is already inaccurate!'.\nOverhearing him, Fu-Tzu laughed. 'Here is a sheet of paper. Write down\nthe precise value of the square root of two for me.'\n\nFu-Tzu said 'When you cut against the grain of the wood, much strength\nis needed. When you program against the grain of a problem, much code\nis needed.'\n\nTzu-li and Tzu-ssu were boasting about the size of their latest\nprograms. 'Two-hundred thousand lines', said Tzu-li, 'not counting\ncomments!'. 'Psah', said Tzu-ssu, 'mine is almost a *million* lines\nalready.' Fu-Tzu said 'My best program has five hundred lines.'\nHearing this, Tzu-li and Tzu-ssu were enlightened.\n\nA student had been sitting motionless behind his computer for hours,\nfrowning darkly. He was trying to write a beautiful solution to a\ndifficult problem, but could not find the right approach. Fu-Tzu hit\nhim on the back of his head and shouted '*Type something!*' The student\nstarted writing an ugly solution. After he had finished, he suddenly\nunderstood the beautiful solution.\n\n%% Progression\n\nA beginning programmer writes his programs like an ant builds her\nhill, one piece at a time, without thought for the bigger structure.\nHis programs will be like loose sand. They may stand for a while, but\ngrowing too big they fall apart{Referring to the danger of internal\ninconsistency and duplicated structure in unorganised code.}.\n\nRealising this problem, the programmer will start to spend a lot of\ntime thinking about structure. His programs will be rigidly\nstructured"; 
} 

var para=recluse().split("\n\n"); 
function reduce(func,length,array) 
{ 
    array.forEach(function(c) 
          { 
           length=func(length,c); 
          }); 
    return length; 
} 

<!---TOP LEVEL FUNCTION FOR PROCESSING A VALUE WITH A FUNCTION AND CREATING AN ARRAY OF TH RESULTS------------> 

function map(func,array) 
{ 
    var result_array=[]; 

    array.forEach(function(c) 
          { 
           result_array.push(func(c)); 
          }); 
    return result_array; 
} 

<!-------------THE ALGORITHM FOR FINDING THE EMPHASISED TEXT AND THE FOOTNOTES WITHIN THE TEXT----> 

function fragmenter(text) 
{ 
       var fragments=[]; 

       function indexing(char) 
       { 
        var index=text.indexOf(char); 
        if(index==-1) 
        { 
         return text.length; 
        } 
        else 
        return index; 
       } 

       function ifAtStart(char) 
       { 
        var end=(char,1); 
         if(end==-1) 
         { 
          throw Error("No enclosing "+char); 
         } 
        var part=text.slice(1,end); 
        text=text.slice(end+1); 
        return part; 
       } 

       function normalText(text) 
       { 
        var end=reduce(Math.min,text.length,map(indexing,["*","{"]));//to check that the index is within the text only 
        var part=text.slice(0,end); 
        var text=text.slice(end); 
        return part; 
       } 

       while(text!="")  <!---4------> 
       { 
        if(text.charAt(0)=="*") 
        { 
         fragments.push({type:"emphasised", 
             content:isAtFirst("*")}); 
        } 
        else if(text.charAt(0)=="{") 
        { 
         fragments.push({type:"footnotes", 
             content:isAtFirst("}")}); 
        } 
        else 
        { 
         fragments.push({type:"Normal", 
             content:normalText(text)}); 
        } 
       } 
return fragments;    
} 

console.log(fragmenter(para[1])); 
+4

也許不相關,也許不是,但你不能(好吧,不應該)在JavaScript中使用HTML樣式的註釋。 '/ *這是一條評論* /' 'window.location =「http://google.com」//這是一條評論# –

+0

@MarkusUnterwaditzer我的印象是,這只是爲了問題,但以防萬一,這可能是有用的! –

+0

@MarkusUnterwaditzer我知道。只是隨HTML一起編碼,所以我輸入。謝謝你提醒。 – user1720527

回答

6

有:

while(text!="") 
{ 
    // some code that doesn't change "text" 
} 

有沒有辦法讓文字在循環改變,因此你的代碼無法完成。

如果你想遍歷字符,你可以這樣做:

for (var i=0; i<text.length; i++) { 
    if (text.charAt(i)=="*") 

但我不知道,如果你錯過了一個事實,當你調用normalText(text),該normalText功能無法改變的變量它有,但只有它自己的。你必須分享文字。一個解決方案是這樣的:

function normalText() // REMOVED text SO IT USES THE ONE OF THE ENCLOSING SCOPE 
      { 
       var end=reduce(Math.min,text.length,map(indexing,["*","{"]));//to check that the index is within the text only 
       var part=text.slice(0,end); 
       var text=text.slice(end); 
       return part; 
      } 

while(text!="")   
      { 
       if(text.charAt(0)=="*") 
       { 
        fragments.push({type:"emphasised", 
            content:isAtFirst("*")}); 
       } 
       else if(text.charAt(0)=="{") 
       { 
        fragments.push({type:"footnotes", 
            content:isAtFirst("}")}); 
       } 
       else 
       { 
        fragments.push({type:"Normal", 
         content:normalText()}); // REMOVED PASSING TEXT 
       } 
      } 
+0

啊,'while(){/*...*/}'循環,偶爾偶爾會發生JavaScript編碼器的禍害。 –

+2

如果你的代碼掛起總是尋找循環或遞歸首先成爲罪魁禍首,那麼考慮其他領域。 – TheZ

+0

那麼我該如何改變它? – user1720527

相關問題