如何在 Beaufort Scale 和 M/S 之间转换风速,反之亦然 Javascript?
How to convert windspeed between Beaufort Scale and M/S and vice versa in Javascript?
我正在尝试创建一个函数,将米每秒 (m/s) 转换为 Javascript 中的 Beaufort scale。我可以使用一系列 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;
}
我想将其替换为使用正确公式自动计算的函数。有谁知道如何在没有多个 if 语句或 switch case 的情况下实现这一点?
好吧,看了好几篇文章,似乎有一个公式可以计算博福特往返m/s。我将用我制作的几个函数来回答我自己的 post。
计算 m/s 为 beaufort:
function msToBeaufort(ms) {
return Math.ceil(Math.cbrt(Math.pow(ms/0.836, 2)));
}
msToBeaufort(24.5);
output: 10
计算博福特为 m/s:
function beaufortToMs(bf){
return Math.round(0.836 * Math.sqrt(Math.pow(bf, 3)) * 100)/ 100;
}
beaufortToMs(3)
output: 4.34
我知道这是一个难得的话题,但希望这对某人有所帮助。
我正在尝试创建一个函数,将米每秒 (m/s) 转换为 Javascript 中的 Beaufort scale。我可以使用一系列 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;
}
我想将其替换为使用正确公式自动计算的函数。有谁知道如何在没有多个 if 语句或 switch case 的情况下实现这一点?
好吧,看了好几篇文章,似乎有一个公式可以计算博福特往返m/s。我将用我制作的几个函数来回答我自己的 post。
计算 m/s 为 beaufort:
function msToBeaufort(ms) {
return Math.ceil(Math.cbrt(Math.pow(ms/0.836, 2)));
}
msToBeaufort(24.5);
output: 10
计算博福特为 m/s:
function beaufortToMs(bf){
return Math.round(0.836 * Math.sqrt(Math.pow(bf, 3)) * 100)/ 100;
}
beaufortToMs(3)
output: 4.34
我知道这是一个难得的话题,但希望这对某人有所帮助。