首页 > 解决方案 > 级联 JS 函数 - 如何将变量从一个函数传输到另一个函数

问题描述

我正在尝试重新创建一个非常简单的平铺计算器:

第 1 步 - 收集一个区域的长度和宽度并返回该区域以用瓷砖覆盖。

第 2 步 - 收集瓷砖的长度和宽度以确定它们的大小。

第 3 步 - 将区域除以瓷砖大小并计算需要多少特定尺寸的瓷砖。

// First step is to calculate the area to tile
function surfaceArea(surfaceWidth, surfaceLength, surfaceUnit) {
    console.log( Math.ceil( surfaceWidth * surfaceLength ) + surfaceUnit );
    return Math.ceil( surfaceWidth * surfaceLength );
}

// Second step is to calculate the size of the tile
function tileArea(tileWidth, tileLength, tileUnit) {
    console.log( Math.ceil( tileWidth * tileLength ) + tileUnit );
    return Math.ceil( tileWidth * tileLength );
}

//Third step is to calculate how many tiles are required to cover the surface area
function tileAmount(surfaceArea, tileArea) {
    console.log( Math.ceil( surfaceArea * 10000 / tileArea ) );
    return Math.ceil( surfaceArea * 10000 / tileArea );
}

let surfaceWidth = prompt('Please enter the width of the surface in metres', 'For example: 5');
let surfaceLength = prompt('Please enter the length of the surface in metres', 'For example: 5');
let surfaceUnit = 'm²';
let sqm = surfaceArea(surfaceWidth, surfaceLength, surfaceUnit);

let tileWidth = prompt('Please enter the width of the tile in centimetres', 'For example: 30');
let tileLength = prompt('Please enter the length of the tile in centimetres', 'For example: 30');;
let tileUnit = 'cm²';
let tile = tileArea(tileWidth, tileLength, tileUnit);

tileAmount();

我不知道如何捕获函数 1 和 2 中的计算并将它们调用到函数 3 中以彼此相除。

标签: javascriptfunctioncallback

解决方案


不会简单

let amount = tileAmount(sqm, tile);

做这份工作?


推荐阅读