javascript - 级联 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 中以彼此相除。
解决方案
不会简单
let amount = tileAmount(sqm, tile);
做这份工作?
推荐阅读
- javascript - 在没有 function 关键字(或箭头运算符)的对象文字中声明的函数的兼容性
- applescript - 在新创建的提醒中填写 URL
- python - 在一个轴上查找 3D 数组中的最小值,并将另一个 3D 数组中的不对应值替换为 0 而无需在 python 中循环
- javascript - Javascript 用 Wordpress 确定页面类型?
- c# - 当另一个 Texblock 值更改时更改 Texblock 样式
- python - 我在抓取第二个元素时遇到了一些问题
- mysql - 使用多对多关系从 MySQL 表中删除重复项
- javascript - 将 .env 常量传递给内部目录
- functional-programming - 为什么 ocaml 有可变数组?
- python - sklearn:使用 Pipeline 和 TransformedTargetRegressor 缩放 x(数据)和 y(目标)