Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

JavaScript

Need some explanations on functions and returns to better understand.

In this example code block,

const multiplyByNineFifths = (celsius) => {
  return celsius * (9/5);
};

const getFahrenheit = (celsius) => {
  return multiplyByNineFifths(celsius) + 32;
};

console.log('The temperature is ' + getFahrenheit(15) + '°F');
// Output: The temperature is 59°F

Why do we nest it in this way? Why would it not be getFahrenheit function then multiplyByNineFifths?

1 Answer

First, just going to format the example code you gave, as a reference:

const multiplyByNineFifths = (celsius) => { return celsius * (9/5); };

const getFahrenheit = (celsius) => { return multiplyByNineFifths(celsius) + 32; };

console.log('The temperature is ' + getFahrenheit(15) + '°F'); // Output: The temperature is 59°F

In the above code, we only need to call getFahrenheit because the getFahrenheit function calls multiplyByNineFifths inside its code already - there's no need to call it separately.

return multiplyByNineFifths(celsius) + 32;

In this line, it will take the celsius value given and pass it to multiplyByNineFifths, which will give us celsius * 9/5. Then, it adds 32, giving us the Fahrenheit value.

As far as why it is nested this way, it doesn't necessarily have to be. You could just have a function like this:

const getFahrenheit = (celsius) => { 
return celsius * (9/5) + 32; 
};

The above would accomplish the same thing - my guess is that the original code was intended as an example of how you can call a function inside another function. If more complicated processes are being performed, it can be helpful to break them up into separate functions and allow those functions to call each other to complete the process - this allows for each to be tested separately, and possibly used separately if they would be useful on their own elsewhere.

Hope this helps!

Thanks Katie. I understand the refactored code much better. If I were going to create this code myself, their example isn't how I would think to do it. It seems like this is just a bad example of nesting functions.

No problem - I'm glad it helped. Honestly, I'm not a huge fan of that nesting either - I can only assume that it's meant as an example that you can, not necessarily that you should in this case. Sometimes things are complex enough that you want to break it up, but I think taking a Celsius value and spitting a Fahrenheit value back out is simple enough, and arguably more useful than a conversion function that requires another function just to do some multiplication.

As an example of when you might want to nest functions, let's say you're trying to print weather information that is currently in metric measurements in imperial measurements instead. Maybe you'd have one function to print and format the text and one to convert:

const getImperial = (celsius, kph) => { 
   let imperial = new Array();
   imperial[0] = celsius * (9/5) + 32;
   imperial[1] = kph * 0.62;
   return imperial; 
};

const printWeatherImperial = (celsius, kph) => {
   let imperial = getImperial(celsius, kph);
   console.log('The temperature is ' + imperial[0] + '°F and the wind speed is ' + imperial[1] + 'miles per hour');
};

printWeatherImperial(15, 22);