🎓 Top 15 Udemy Courses (80-90% Discount): My Udemy Courses - Ramesh Fadatare — All my Udemy courses are real-time and project oriented courses.
▶️ Subscribe to My YouTube Channel (178K+ subscribers): Java Guides on YouTube
▶️ For AI, ChatGPT, Web, Tech, and Generative AI, subscribe to another channel: Ramesh Fadatare on YouTube
Introduction
Finding the maximum and minimum elements in an array is a common task in programming. JavaScript provides methods like Math.max() and Math.min() that allow us to easily determine the largest and smallest numbers in an array. This program demonstrates how to find both the maximum and minimum elements in an array.
Problem Statement
Create a JavaScript program that:
- Accepts an array of numbers.
- Finds the maximum and minimum elements in the array.
- Returns and displays both the maximum and minimum values.
Example:
Input:
[5, 3, 8, 1, 9]Output:
Maximum: 9, Minimum: 1Input:
[12, 45, 23, 9]Output:
Maximum: 45, Minimum: 9
Solution Steps
- Read the Input Array: Provide an array of numbers either as user input or directly in the code.
- Find the Maximum and Minimum Elements: Use
Math.max()andMath.min()with the spread operator to find the maximum and minimum values. - Display the Result: Print the maximum and minimum values.
JavaScript Program
// JavaScript Program to Find the Maximum and Minimum Element in an Array
// Author: https://www.javaguides.net/
function findMaxMin(arr) {
// Step 1: Find the maximum element
let max = Math.max(...arr);
// Step 2: Find the minimum element
let min = Math.min(...arr);
// Step 3: Return the result
return { max, min };
}
// Example input
let array = [5, 3, 8, 1, 9];
let { max, min } = findMaxMin(array);
console.log(`The maximum element is: ${max}`);
console.log(`The minimum element is: ${min}`);
Output
The maximum element is: 9
The minimum element is: 1
Example with Different Input
let array = [12, 45, 23, 9];
let { max, min } = findMaxMin(array);
console.log(`The maximum element is: ${max}`);
console.log(`The minimum element is: ${min}`);
Output:
The maximum element is: 45
The minimum element is: 9
Explanation
Step 1: Find the Maximum Element
- The
Math.max()function is used with the spread operator (...arr) to find the largest number in the array. The spread operator expands the array into individual elements, allowingMath.max()to operate on them.
Step 2: Find the Minimum Element
- Similarly, the
Math.min()function is used with the spread operator to find the smallest number in the array.
Step 3: Return and Display the Result
- The function returns an object containing both the
maxandminvalues, which are then printed usingconsole.log().
Conclusion
This JavaScript program demonstrates how to find both the maximum and minimum elements in an array using Math.max() and Math.min() with the spread operator. This approach is efficient and handles arrays of any size, making it a versatile solution for finding the largest and smallest values in an array.
My Top and Bestseller Udemy Courses. The sale is going on with a 70 - 80% discount. The discount coupon has been added to each course below:
Build REST APIs with Spring Boot 4, Spring Security 7, and JWT
[NEW] Learn Apache Maven with IntelliJ IDEA and Java 25
ChatGPT + Generative AI + Prompt Engineering for Beginners
Spring 7 and Spring Boot 4 for Beginners (Includes 8 Projects)
Available in Udemy for Business
Building Real-Time REST APIs with Spring Boot - Blog App
Available in Udemy for Business
Building Microservices with Spring Boot and Spring Cloud
Available in Udemy for Business
Java Full-Stack Developer Course with Spring Boot and React JS
Available in Udemy for Business
Build 5 Spring Boot Projects with Java: Line-by-Line Coding
Testing Spring Boot Application with JUnit and Mockito
Available in Udemy for Business
Spring Boot Thymeleaf Real-Time Web Application - Blog App
Available in Udemy for Business
Master Spring Data JPA with Hibernate
Available in Udemy for Business
Spring Boot + Apache Kafka Course - The Practical Guide
Available in Udemy for Business
Comments
Post a Comment
Leave Comment