find the largest number in the following list of numbers: [1, 3, 4, 2, 5] in javascript

Here is one way to find the largest number in the given list of numbers using the built-in Math.max() function and the Array.reduce() method:

index.tsx
const numbers = [1, 3, 4, 2, 5];
const largestNumber = numbers.reduce((prev, current) => {
  return Math.max(prev, current);
});
console.log(largestNumber); // Output: 5
170 chars
6 lines

In the above code, we first define an array of numbers. Then, we use the reduce() method to iterate over each value in the array and compare it with the previous value. At each step, we return the maximum of the two values using the Math.max() function. Finally, the reduce() method returns the largest number in the array. We assign this value to a variable called largestNumber and log it to the console. The output shows that the largest number in the array is 5.

gistlibby LogSnag