簡體   English   中英

如何在 Javascript 中在 Beaufort Scale 和 M/S 之間轉換風速,反之亦然?

[英]How to convert windspeed between Beaufort Scale and M/S and vice versa in Javascript?

我正在嘗試創建一個函數,以在 Javascript 中將米每秒 (m/s) 轉換為Beaufort 比例 我可以使用一系列 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 的情況下實現這一目標?

好的,所以在閱讀了幾篇文章之后,似乎有一個公式可以計算 Beaufort 與 m/s 之間的關系。 我將用我所做的幾個功能來回答我自己的帖子。

計算 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

我知道這是一個罕見的話題,但希望這對某人有所幫助。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM