簡體   English   中英

TypeError:無法調用未定義的方法“ toFixed”,JavaScript錯誤

[英]TypeError: Cannot call method 'toFixed' of undefined, JavaScript Error

我正在上在線編碼課程,但不斷收到此錯誤:

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));

非常感謝您的幫助-摩根

您應該return tileCount;

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

參見jsFiddle

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM