Monika Verma Monika Verma - 7 months ago 12
Javascript Question

What to use Map , Reduce and filter in js to convert fahrenheit to celcius

I have an array of temperatures and i have to convert them to degree celcius , i was told i need to use map/reduce in javascript . I have looked through the documentation but i cant seem to figure out how to do it.
Here is my array :

var fahrenheit = [0, 32, 45, 50, 75, 80, 99, 120];

Answer

This should work

  var celcius = fahrenheit.map(function(elem) {
        return Math.round((elem - 32) * 5 / 9);
    });

or in ES6

fahrenheit.map(elem => Math.round((elem - 32) * 5 / 9));

and you get

celcius //  [-18, 0, 7, 10, 24, 27, 37, 49]
Comments