Find the Maximum & Minimum of an Array in JavaScript | JavaScript Interview Questions



Find the Maximum & Minimum of an Array in JavaScript | JavaScript Interview Questions

Find the Maximum & Minimum of an Array in JavaScript | JavaScript Interview Questions

Write a function to find the maximum and minimum number in an array There are several ways to find the maximum and minimum of an array in JavaScript. We can use Math max and Math min functions.

Create an array called “numbers”. To get the largest value of the array use the built-in Math max function. Use “…” (spread operator) to spread the array elements as individual arguments to the Math max function. Similarly, use math min function to get the minimum value of array. Display the minimum and maximum from array by using min and max variables.

We can also use reduce method to do the same. The reduce method applies ‘Math max’ function to each element of the array and accumulates the result. Reduce method takes two arguments, the accumulator and the current value. The accumulator is the result of the previous function call, and the current value is the current element being processed. The first call to the function uses the first two elements of the array as arguments.

We can use for loop to do that as well. Let’s define a function called “findMaxMin” that takes in an array of numbers as its parameter. Declare two variables called “max” and “min”. Assign the first element of the “numbers” array as their initial value. Use a for loop to iterate through the elements of the “numbers” array starting from the second element.

Check if each element is greater than the current value of the “max” variable. If it is, assign that element to the “max” variable. If the element is not greater than the current value of “max”, check if it is less than the current value of “min”. If it is, assign that element to the “min” variable.

After the loop completes, return a string containing the maximum and minimum values found in the array. Invoke the findMaxMin function passing the numbers array and you will get the minimum and maximum values of array.

Using ‘math max’ and ‘math min’ methods are the most efficient way of finding the max and min elements in an array and using for loop is not efficient.

So this is how we can find the maximum and minimum values in an array using javascript.

Our tutorials help you to improve your career growth, perform better in your job and make money online as a freelancer. Learn the skills to build and design professional websites, and create dynamic and interactive web applications using JavaScript, or WordPress. Our tutorials are tailored to help beginners and professionals alike. Whether you’re just starting in the field or you’re looking to expand your knowledge, we’ve got something for you. Join us on this journey to becoming a skilled web developer. Subscribe to our channel and let’s get started!

Thank You!
👍 LIKE VIDEO
👊 SUBSCRIBE
🔔 PRESS BELL ICON
✍️ COMMENT

⚡Channel: https://www.youtube.com/@webstylepress
⚡Website: https://www.webstylepress.com
⚡FaceBook: https://www.facebook.com/webstylepress
⚡Twitter: https://twitter.com/webstylepress
⚡GitHub: https://github.com/webstylepress
#js #javascript #challenge #codingchallenge #WebStylePress #WebDevelopment #javascriptinterviewquestions #javascripttutorial #leetcode #coding #programming #computerscience #algorithm

Comments are closed.