Climate Control Explainer


#1

Aim of the puzzle: Use the .map() array method to create a new array with the results of calling a function on each element of a separate array.
Walk through of solution: The .map() array method is a useful way to create a new array using the elements of another array.

.map() takes a callback function as an argument, and will then iterate (loop) through the array it is called upon using each element as the argument for that function.

The celsius array contains 4 numbers representing temperatures. We want to convert these temperatures to Fahrenheit.

On the next line, .map() is used on the celsius array, and an arrow function is given as an argument. temp is the parameter of this function, which returns 32 + 9/5 * temp to the new fahrenheit array.

As .map() iterates through the elements of celsius, each number becomes temp, goes through the conversion formula 32 + 9/5 * temp, and it returned to the new fahrenheight array.

The first element in celsius is the number 23. This means that the first element in fahrenheit will be the result of 32 + 9/5 * 23, which is 73.4.

The second element in celsius is 27, which means that the second element in fahrenheit will be 32 + 9/5 * 27, or 80.6.

At the bottom of the code, the For…of loop will log each element of the fahrenheit array to the console.

Important: If you are experiencing an issue where your formula looks like 32 + 9 / (5 * temp), this is because the 5 was highlighted when the * key was pressed. To fix this issue, tap on the space around the / to highlight the entire 9 / 5, THEN press the * key.

Sample code solution:

let celsius = [23, 27, 22, 18];

let fahrenheit = celsius.map(temp => {
    return 32 + 9/5 * temp;
});

for (let temp of fahrenheit) {
    console.log(temp);
}

Javascript Concepts: Arrays, Array Methods, .map(), Iteration, For…of Loops, console.log