I want to display miles-per-hour and kilometres per hour together. is that possible?
How should I implement the kilometres per hour calculation and display it together?
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()
You returning to early, add the kph one and concatenate it.
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))
You can return an object containing both mph and kph, then display it any way you want;
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
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.