2015-06-24 128 views
0

一直試圖在書練習上工作,我目前正在創建一個多功能函數。這裏是我的代碼:第1行意外的輸入結束

<!DOCTYPE html> 
<html> 
    <head> 
     <title>Chapter 4, Exercise 1</title> 
    </head> 
<body> 
    <script type="text/javascript"> 

    var funcNumber = parseInt(prompt("Please Enter Number to multiply:"), 10); 
    var funcMultiplier = parseInt(prompt("Start Multiplication from:"), 10); 
    var funcEnd = parseInt(prompt("Up to:"), 10); 
    var funcResult; 


    function multiplesOf(funcNumber, funcMultiplier) { 
     for(funcMultiplier;funcMultiplier<=funcEnd;funcMultiplier++){ 
       funcResult = funcNumber * funcMultiplier; 
       return funcResult; 
       document.write(funcNumber + "*" + funcMultiplier + "=" + funcResult  + "<br/>"); 
      } 
     } 
    } 

    function multiplesOf(funcNumber, funcMultiplier) 



    </script> 
    </body> 
</html> 

但在瀏覽器中運行它,它說,在HTML「輸入意外結束」:1

回答

0

的問題是在這條線

function multiplesOf(funcNumber, funcMultiplier) 

你定義一個函數,但沒有和那是一個語法錯誤。

如果你想打電話multiplesOf,正確syntaxt將

var result = multiplesOf(funcNumber, funcMultiplier); 

代碼中的其他一些問題(添加評論)

function multiplesOf(funcNumber, funcMultiplier) { 
    for(funcMultiplier;funcMultiplier<=funcEnd;funcMultiplier++){ 
      funcResult = funcNumber * funcMultiplier; 
      return funcResult; // this stops execution flow, nothing else is executed 
      document.write(funcNumber + "*" + funcMultiplier + "=" + funcResult + "<br/>"); 
     } 
    } 
} // you missed this 
+0

有趣的是我刪除;故意,因爲它也發出錯誤「意外的令牌;」 – defc0de

+0

感謝您的回報。我剛剛刪除它,現在一切正常。 – defc0de

0

的問題是與線:

function multiplesOf(funcNumber, funcMultiplier) 

你的代碼只是聲明該函數,它給了錯誤喲你看到了。

你想要的是一個函數調用。嘗試將函數的輸出設置爲一個變量,然後使用它。

var output = multiplesOf(funcNumber, funcMultiplier); 
alert(output); 
+0

它現在正在工作,但很快的問題,爲什麼函數調用錯誤?我其實有函數multiplesOf(funcNumber,funcMultiplier);作爲我的函數調用,但它發出「意外的令牌;」 – defc0de

相關問題