Skip to content

Instantly share code, notes, and snippets.

@manavm1990
Created February 5, 2021 21:55
Show Gist options
  • Save manavm1990/950b5bfd1772654f60d0018817e1c651 to your computer and use it in GitHub Desktop.
Save manavm1990/950b5bfd1772654f60d0018817e1c651 to your computer and use it in GitHub Desktop.
/**
* When I click and start typing in either input,
* update the other's input value with the converted temperature.
*
* HINTS: 1. 'keyup' event
* 2. Use function keyword so that you can use 'this'
*/
const converters = {
toCelsius(fahrenheit) {
return ((fahrenheit - 32) * 5) / 9
},
toFahrenheit(celsius) {
return (celsius * 9) / 5 + 32
},
}
export default (temperature, conversion) => {
const parsedTemp = parseFloat(temperature)
if (Number.isNaN(parsedTemp)) {
return "Not something we can convert! 😞"
}
return Math.round(converters[conversion](parsedTemp) * 1000) / 1000
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment