2013-04-21 92 views
0

林參加在線課程編碼和我不斷收到此錯誤:類型錯誤:無法調用「toFixed」的定義,JavaScript錯誤

Line 18, TypeError: Cannot call method 'toFixed' of undefined 

不知道什麼錯誤。下面是完整的代碼:

//Calculate the area of a rectangle 
function calculateArea(length, width) { 
    return length * width; 
} 

function calculateTileCount(tileWidth,length,width) 
{ 
    var tileArea = tileWidth * tileWidth 
    var tileCount = calculateArea(length, width)/tileArea; 
} 

var bathroom1 = calculateTileCount(0.3, 5, 3); 
var bathroom2 = calculateTileCount(0.3, 6.5, 3.5); 
var bathroom3 = calculateTileCount(0.45, 9, 4); 
var bathroom4 = calculateTileCount(0.45, 11, 4); 

console.log('Bathroom Tiling' + 
    '\nBathroom 1: ' + bathroom1.toFixed(0) + 
    '\nBathroom 2: ' + bathroom2.toFixed(0) + 
    '\nBathroom 3: ' + bathroom3.toFixed(0) + 
    '\nBathroom 4: ' + bathroom4.toFixed(0)); 

任何幫助是非常讚賞, -Morgan

+1

你的函數什麼都不返回 – zerkms 2013-04-21 04:31:58

+1

更準確地說,你的函數返回'undefined'。 – nnnnnn 2013-04-21 04:40:41

回答

3

你應該return tileCount;

function calculateTileCount(tileWidth,length,width) 
{ 
    var tileArea = tileWidth * tileWidth 
    var tileCount = calculateArea(length, width)/tileArea; 
    return tileCount; 
} 

jsFiddle

相關問題