【问题标题】:How do I calculate average wind speed and direction from a list of values over a period in javascript如何从javascript中一段时间​​内的值列表计算平均风速和风向
【发布时间】:2023-01-09 18:04:20
【问题描述】:

如何根据一段时间内的值列表计算平均风速和风向?

const directions = ["S","S","NE","SW","NNE","N"];
const speeds     = [11,9,9,7,18,10]

网上有很多资源可以计算 A 和 B 之间的平均值(javascriptMYSQLSQLServer),但没有什么是简单易懂和易于使用的。

【问题讨论】:

  • +((numbers.reduce((a, b) => a + b, 0) / numbers.length).toFixed(0)) 是为什么比提到的其他解决方案更容易?
  • 你能澄清一下这个问题吗?似乎您想将罗盘点列表转换为以度为单位的平均值。那是对的吗?然后什么?你想要平均风速除以平均度数?也许如果你解释你为什么要这样做,它会帮助人们找出最好的方法。

标签: javascript weather


【解决方案1】:

我认为这不是答案,但可以提供帮助


    function getAverageNumber(numbers){
        return +((numbers.reduce((a, b) => a + b, 0) / numbers.length).toFixed(0))
    }
    function getWindDirection(d){
        if(d===0 || d===360) return "N";
        if(d===90) return "E";
        if(d===180) return "S";
        if(d===270) return "W";
        if (d>0 && d<90) return "NE";
        if (d>90 && d<180) return "SE";
        if (d>180 && d<270) return "SW";
        if (d>270 && d<360) return "NW";
    } 
    function getWindDegrees(w){
        if(w==="N") return 0;
        if(w==="NE") return 45;
        if(w==="E") return 90;
        if(w==="SE") return 135;
        if(w==="S") return 180;
        if(w==="SW") return 225;
        if(w==="W") return 270;
        if(w==="NW") return 315;
    } 
    function getAverageWind(_directions, _speeds){
        const averageDirection = getAverageNumber(_directions.map(getWindDegrees));
        const averageSpeed = getAverageNumber(_speeds);
        return { speed: averageSpeed, direction: getWindDirection(averageDirection) }
    }

const result = getAverageWind(directions, speeds)

// i don't this answer is entirely accurate
console.log(`${result.speed} km/h ${result.direction}`)

【讨论】:

  • “我不认为这是答案……”- 然后不要将其作为答案发布...o.O
猜你喜欢
  • 2016-03-05
  • 1970-01-01
  • 2021-08-05
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-12-11
相关资源
最近更新 更多