在 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