Code I did for Codewars...

·

1 min read

One of the problems I did on Codewars today was to add the two smallest numbers in an array. The array was [19, 5, 42, 2, 77]. It has been literally months since I did this so my mind was confused. Instead of googling and typing the answer down, I wanted to learn how the problem was solve. Here is the problem:

function sumTwoSmallestNumbers(numbers) // here is the function

let arr = numbers.sort((a,b) => a-b)// using the .sort method, the array was put in order so now it's [2,5,19,42,77]

return arr[0] + arr[1]

[0] is 2 and [1] is 5. Since they are added together, the answer is 7.