Find the Difference in Age between Oldest and Youngest Family Members - Codewars 8KYU

"At the annual family gathering, the family likes to find the oldest living family member’s age and the youngest family member’s age and calculate the difference between them.

You will be given an array of all the family members' ages, in any order. The ages will be given in whole numbers, so a baby of 5 months, will have an ascribed ‘age’ of 0. Return a new array (a tuple in Python) with [youngest age, oldest age, difference between the youngest and oldest age]."

We're given a function with the parameter with the name of ages:

Since we're trying to figure out what the age gap is between the oldest living relative, and the youngest. I figured we could tackle the problem by using two methods, Math.max() and Math.min(). Math.max() returns the largest number in an array and Math.min() returns the the smallest number in an array.

By using those, we can assign a variable oldest to Math.max(), and a variable youngest to Math.min(). With both taking in a spread operator of (...ages).

A spread operator, according to the MDN,

"Spread syntax (...) allows an iterable such as an array expression or string to be expanded in places where zero or more arguments (for function calls) or elements (for array literals) are expected, or an object expression to be expanded in places where zero or more key-value pairs (for object literals) are expected. "

After successfully getting access to the oldest and youngest ages in the family. Now we can assign a variable difference, that will subtract the variables oldest from the youngest.

Then, we can return an array containing the difference between the oldest and youngest ages in the array.