Find the Maximum & Minimum of an Array in JavaScript | Learn JavaScript & Algorithms

preview_player
Показать описание
Find the maximum & minimum of an array in javascript. Write a function to find the maximum and minimum number in an array using javascript. 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.

* Full Playlist (Coding Challenge, Interview Questions & Leetcode) *

It can be a good javascript interview question or frontend interview question. You may not be required to solve it on paper or whiteboard but the interviewer may ask you to give an idea on how to approach this algorithm. If you have an understanding of how to solve this problem or approach this algorithm, you will be able to answer it and get your next job as a frontend developer or full-stack developer.

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

#js #javascript #challenge #codingchallenge #WebStylePress #WebDevelopment #javascriptinterviewquestions #javascripttutorial #leetcode #coding #programming #computerscience #algorithm
Рекомендации по теме
Комментарии
Автор

I really love your video. Can you please do more on data structures and algorithm more on with lucid details and explanations ...thank you so much man

sohailshah
Автор

Thank you a lot. Function findMaxMin could find max & min from an array 3.9 million dates in the browser. While Math.min() after 100k got the "maximum call stack size exceeded" error

jdrncoq