2017-01-03 25 views
0

我努力使產生的數學表的任何數量的像這樣的程序:發行與製作數量的表項目(如12×1 = 12)在Javascript

3×1 = 3

3 ×2 = 6

3×3 = 9

3×4 = 12

我需要用戶輸入:

(1)它們需要(例如 - 3)表中的所有數

(2)指定開始點(例如 - 1)

(3)指定的結束點(例如 - 4)

我有缺陷的代碼到目前爲止如下:

function isitanumber(numb){ 
 
     while (isNaN(numb) == true){ 
 
      \t \t numb = parseInt(prompt("Please add a valid number","5")); 
 
      \t } 
 
     } 
 
    
 
    
 
    function mytable (thenum, first, second){ 
 
     for (var i=first; i<=second; i++){ 
 
     \t var y = thenum*i; 
 
     \t document.write(thenum + " x " + i + " = " + y + "</br>"); 
 
     } 
 
     } 
 
    
 
    
 
    var mynum = parseInt(prompt("Enter the number you wish to have the table for", "40")); 
 
    mynum = isitanumber(mynum); 
 
    
 
    
 
    var startpoint = parseInt(prompt("Enter the startpoint of the table", "1")); 
 
    mynum = isitanumber(startpoint); 
 
    
 
    var endpoint = parseInt(prompt("Enter the endpoint of the table", "10")); 
 
    mynum = isitanumber(endpoint); 
 
    
 
    
 
    mytable(mynum,startpoint,endpoint);

+1

什麼問題? – SLaks

+0

每當我在第一個提示中輸入字母,然後在第二個提示中輸入數字,它就會給我NaN。具體來說,我需要找出如何訪問用戶輸入的最後一個值,其中isNaN爲false。 – nishkaush

+0

使用調試器來查看你的變量是什麼。 – SLaks

回答

1

isitanumber回報undefined(每個時間分配給mynummynum = isitanumber(...)))沒有return語句的函數返回的默認值。因此,mynum包含undefined值。

爲完成循環的NaN·您應該返回變量(並將其分配給相應的變量):

function isitanumber(numb) 
 
{ 
 
    while (isNaN(numb)) { 
 
      numb = parseInt(prompt("Please add a valid number","5")); 
 
    } 
 
    return numb; 
 
} 
 

 
function mytable (num, start, end) 
 
{ 
 
    for (var i = start; i <= end; i++) { 
 
     var y = num * i; 
 
     document.write(num + " x " + i + " = " + y + "</br>"); 
 
    } 
 
} 
 

 
var mynum = parseInt(prompt("Enter the number you wish to have the table for", "40")); 
 
mynum = isitanumber(mynum); 
 

 
var startpoint = parseInt(prompt("Enter the startpoint of the table", "1")); 
 
startpoint = isitanumber(startpoint); 
 

 
var endpoint = parseInt(prompt("Enter the endpoint of the table", "10")); 
 
endpoint = isitanumber(endpoint); 
 

 
mytable(mynum, startpoint, endpoint);

+0

非常感謝您的詳細回覆!在過去的8個小時裏,這個代碼已經被卡住了,我想我現在腦子已經死了。一個簡單的問題 - 爲什麼添加「返回」使其工作?它在代碼的流程中發生了什麼變化?謝謝 – nishkaush

+0

@nishkaush:好吧,沒有它,你的函數不會返回一個值。但是你試圖使用函數返回的值。你能看到這不起作用嗎? –

+0

對不起,我是JS的新手,仍然在搞清楚事情是如何工作的。我被告知不使用返回,因爲它結束函數和循環,應該是最後的選擇。但顯然它在這裏保存了我的代碼。 – nishkaush

0

你做了如下錯誤:

函數返回值, 指定起點變量, 指定終點變量

function isitanumber(numb){ 
 
    while (isNaN(numb) == true){ 
 
      numb = parseInt(prompt("Please add a valid number","5")); 
 
     } 
 
     return numb; 
 
    } 
 

 

 
function mytable (thenum, first, second){ 
 
    for (var i=first; i<=second; i++){ 
 
     var y = thenum*i; 
 
     document.write(thenum + " x " + i + " = " + y + "</br>"); 
 
    } 
 
    } 
 

 

 
var mynum = parseInt(prompt("Enter the number you wish to have the table for", "40")); 
 
mynum = isitanumber(mynum); 
 

 

 
var startpoint = parseInt(prompt("Enter the startpoint of the table", "1")); 
 
startpoint = isitanumber(startpoint); 
 

 
var endpoint = parseInt(prompt("Enter the endpoint of the table", "10")); 
 
endpoint = isitanumber(endpoint); 
 

 

 
mytable(mynum,startpoint,endpoint);

相關問題