如何在 Javascript 中在蒲福风级和 M/S 之间转换风速,反之亦然?

Mr *_*r L 2 javascript formula

我正在尝试创建一个函数,将 Javascript 中的米每秒 (m/s) 转换为蒲福氏尺度。我可以使用一系列 if 语句来完成此操作,但我更愿意将其替换为公式来为我动态计算。

到目前为止,我的研究使我能够实现以下目标:

function beaufort(ms) {
    ms = Math.abs(ms);
    if (ms <= 0.2) {
        return 0;
    }
    if (ms <= 1.5) {
        return 1;
    }
    if (ms <= 3.3) {
        return 2;
    }
    if (ms <= 5.4) {
        return 3;
    }
    if (ms <= 7.9) {
        return 4;
    }
    if (ms <= 10.7) {
        return 5;
    }
    if (ms <= 13.8) {
        return 6;
    }
    if (ms <= 17.1) {
        return 7;
    }
    if (ms <= 20.7) {
        return 8;
    }
    if (ms <= 24.4) {
        return 9;
    }
    if (ms <= 28.4) {
        return 10;
    }
    if (ms <= 32.6) {
        return 11;
    }
    return 12;
}
Run Code Online (Sandbox Code Playgroud)

我想用一个使用正确公式自动计算的函数来替换它。有谁知道如何在没有多个 if 语句或 switch case 的情况下实现这一点?

Mr *_*r L 5

好的,在阅读了几篇文章后,似乎有一个公式可以计算波弗特与 m/s 之间的关系。我将用我所做的几个功能来回答我自己的帖子。

计算到 beaufort 的 m/s:

function msToBeaufort(ms) {
    return Math.ceil(Math.cbrt(Math.pow(ms/0.836, 2)));
}

msToBeaufort(24.5);
output: 10
Run Code Online (Sandbox Code Playgroud)

将波弗特计算为 m/s:

function beaufortToMs(bf){
    return Math.round(0.836 * Math.sqrt(Math.pow(bf, 3)) * 100)/ 100;
}

beaufortToMs(3)
output: 4.34
Run Code Online (Sandbox Code Playgroud)

我知道这是一个罕见的话题,但希望这对某人有帮助。