Kajal Yadav’s Post

Day 5 — Find Largest Number in an Array (JavaScript) Problem Write a function to find the largest number in an array. Example Input: [3, 7, 2, 9, 5] Output: 9 Approach Loop through the array and keep track of the maximum value. Code function findLargest(arr){ let max = arr[0] for(let i = 1; i < arr.length; i++){ if(arr[i] > max){ max = arr[i] } } return max } console.log(findLargest([3,7,2,9,5])) Alternative Approach function findLargest(arr){ return Math.max(...arr) } What I Learned How to track maximum value while iterating through an array. #javascript #frontenddeveloper #codingpractice #webdevelopment

  • text

To view or add a comment, sign in

Explore content categories