开发者问题收集

TypeError:无法调用未定义的方法“toFixed”,JavaScript 错误

2013-04-21
3627

我正在参加在线编码课程,但一直收到此错误:

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个回答

您应该 返回tileCount;

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

请参阅 jsFiddle

2013-04-21