用 JavaScript 根据宽度和屏幕尺寸比例(宽度:高度)查找高度
问题
我们需编写一个 JavaScript 函数,该函数将屏幕宽度作为第一个参数,宽高比 (w:h) 作为第二个参数。基于这两个输入,我们的函数应返回屏幕高度。
示例
以下是代码 −
const ratio = '18:11';
const width = 2417;
const findHeight = (ratio = '', width = 1) => {
const [w, h] = ratio
.split(':')
.map(Number);
const height = (width * h) / w;
return Math.round(height);
};
console.log(findHeight(ratio, width));输出
以下是控制台输出 −
1477
广告
数据结构
网络
RDBMS
操作系统
Java
iOS
HTML
CSS
Android
Python
C 编程
C++
C#
MongoDB
MySQL
Javascript
PHP