同时显示每小时的英里数和每小时的公里数?

问题描述 投票:0回答:2

我想同时显示每小时英里数和每小时公里数。那可能吗?

我应该如何实施每小时公里数的计算并一起显示?

function getMiles (knots) {

    var mph = (knots * 1.15078);
    var speed = Math.round(mph);


    if (speed < 50) {
        return speed + console.log('mhp ');
    }
    if (speed > 50) {
        return speed + console.log('mph , wind can be too strong today ');
    };
}getMiles()
javascript jquery if-statement weather-api
2个回答
1
投票

您回到较早的位置,将kph加一并连接起来。

const wind = knots => {
  const miles = Math.round(knots * 1.15078)
  const kph = Math.round(knots * 1.852)

  return `${miles} mph / ${kph} kph ` + (
    miles > 50 ? 'wind can be too strong today' : ''
  )
}

console.log(wind(80))

0
投票

您可以返回同时包含mph和kph的对象,然后以任何需要的方式显示它;

function getSpeed (knots) {
  const mph = Math.round(knots * 1.15078);
  const kph = Math.round(knots * 1.852000888);

  return { mph, kph };
}

let speed = getSpeed(50);
console.log(`mph: ${speed.mph}, kph: ${speed.kph}`);
// mph: 58, kph: 93
© www.soinside.com 2019 - 2024. All rights reserved.