Today, I learned about the how to print a function output in a template literal, how to use the spread(...) operator to spread the values of an array into separate arguments, how to use an IIFE that takes in a length and width of a rectangle as a parameter and outputs the area.
//Challenge 1
/**
* Create a function called getCelsius() that takes a temperature in Fahrenheit as an argument
* and returns the temperature in celsius. For bonus points,
* write it as a one line arrow function}
*/
//Regular function
function getCelsiusRegular(f) {
const celsius = ((f - 32) * 5) / 9;
return celsius;
}
console.log(getCelsiusRegular(40)); //Output: 4.444444
//Arrow function
const getCelsiusArrow = (f) => ((f - 32) * 5) / 9;
console.log(getCelsiusArrow(50)); //Output: 10
//With template string
console.log(`The temp for ${new Date()} is ${getCelsiusArrow(32)} \xB0C`);
//Output: The temp for Fri Feb 14 2025 10:15:32 GMT+0530 (India Standard Time) is 0 °C
//Challenge 2
/**
* Create an arrow function called minMax() that takes in an array of numbers and returns an
* object with the minimum and maximum numbers in the array.
* Use the Spread(...)operator to spread the values of an array into separate arguments
*/
function minMax(arr) {
const min = Math.min(...arr);
const max = Math.max(...arr);
return {
min,
max,
};
}
console.log(minMax([1, 2, 3, 4, 5]));
//Challenge 3
/***
* Create an IIFE (Immediately Invoked Function Expression) that takes in the length and width
* of a rectangle outputs it to the console in a message as soon as the page loads.
* Area of rectangle = length * width
*/
((length, width) => {
const area = length * width;
const output = `The area of rectangle with ${length} and ${width} is ${area}`;
console.log(output);
})(10, 5); //Output: The area of rectangle with 10 and 5 is 50