The Wayback Machine - https://web.archive.org/web/20240827064011/https://www.geeksforgeeks.org/how-to-find-min-max-values-without-math-functions-in-javascript/
Open In App

How to find min/max values without Math functions in JavaScript ?

Last Updated : 28 Apr, 2023
Comments
Improve
Suggest changes
Like Article
Like
Save
Share
Report
News Follow

In this article, we will learn how to find the minimum and maximum values in an array without using Math functions. We know that Math.max() returns the maximum number passed in an array whereas Math.min() returns the minimum number passed.

Approach: The same functionality of the Math functions can be implemented using a loop that will iterate the numbers present in the array to find maximum and minimum values inside the array.

We will keep checking all the elements in the array using a loop. If we find any element greater than the previous “max” value, we set that value as the max value. Similarly, we keep on checking for any element lesser than the “min” value. If we get any such element, we replace the value of “min” with a smaller value. Thus, using the loop function, we can get the maximum and minimum values entered in an array/list.

Example 1: The below example will demonstrate the above approach.

JavaScript




// Array of numbers where the maximum
// and minimum are to be found
const array = [-1, 2, -5, 8, 16];
 
// Setting a number of the given array as
// value of max and min we choose 0th index
// element as atleast one element should be
// present in the given array
 
let max = array[0], min = array[0];
for (let i = 0; i < array.length; i++) {
 
    // If we get any element in array greater
    // than max, max takes value of that
    // larger number
    if (array[i] > max) { max = array[i]; }
 
    // If we get any element in array smaller
    // than min, min takes value of that
    // smaller number
    if (array[i] < min) { min = array[i]; }
}
console.log("max = " + max);
console.log("min = " + min);


Output: The maximum and the minimum number of the array is found using the approach.

max = 16
min = -5

Example 2: In this example, we will get max and min without function.

Javascript




const arr = [-7,-10,8,6,5,4];
 
let max = arr[0]
let min = arr[0];
for (let i = 0; i < arr.length; i++) {
    // If the element is greater
    // than the max value, replace max
    if (arr[i] > max) { max = arr[i]; }
 
    // If the element is lesser
    // than the min value, replace min
    if (arr[i] < min) { min = arr[i]; }
}
console.log("Max element is: " + max);
console.log("Min element is:"+min);


Output:

Max element is: 8
Min element is:-10


Similar Reads

How to get all property values of a JavaScript Object (without knowing the keys) ?
Method 1: Using Object.values() Method: The Object.values() method is used to return an array of the object's own enumerable property values. The array can be looped using a for-loop to get all the values of the object. Therefore, the keys are not required to be known to get all the property values. Syntax: let valuesArray = Object.values(exampleOb
2 min read
How to map array values without using map method in JavaScript ?
Array elements can be mapped by using looping methods in JavaScript. The map() method creates a new array with the results of the output of a function called for each array element. This can also be implemented using a for loop in JavaScript. Approach 1: For this, we can create two arrays, in which one array contains the array elements that are to
3 min read
PHP Math Functions (is_nan, pow, sqrt, exp, log, log10, log1p, max, min, getrandmax, rand, mt_rand)
11. is_nan() : This function takes any value as argument and checks whether a value is number or not. It returns TRUE (1) if the specified value is 'not a number', otherwise it returns FALSE/NOTHING. Syntax : is_nan(value); Example : C/C++ Code &amp;lt;?php echo is_nan(234) . &amp;quot;\n&amp;quot;; echo is_nan(asin(1.1)) ; ?&amp;gt; Output : 1 Not
4 min read
PHP Math Functions (abs, pi, deg2rad, rad2deg, fmod, floor, ceil, round, is_finite, is_infinite)
As you know PHP is a server side script language and is used for creating dynamic web pages. PHP provides a number of built-in math functions which help in performing several operations while dealing with mathematical data. It has nice support for mathematical processing. No installation is required to use these functions. Some examples of mathemat
4 min read
PHP Math Functions Complete Reference
The predefined math functions in PHP are used to handle the mathematical operations within the integer and float types. These math functions are part of the PHP core. Installation: These functions have not required any installation. The complete list of PHP math functions are given below: Example: Programs illustrate the decbin() function in PHP: C
3 min read
Less.js Math Functions
In this article, we will see the various Math functions that are provided by Less.js to perform various mathematical functions as per the user's requirements within CSS code only. Less.js (Leaner Style Sheets) is an extension to normal CSS which basically enhances the abilities of normal CSS and gives superpowers to it. LESS.js provides the built-i
6 min read
CSS Math Functions
In this article, we will learn about the CSS Math Functions. CSS Math Functions enable you to express CSS numeric values using mathematical expressions directly in your stylesheets. By using CSS Math Functions, we achieve greater flexibility and ease in creating responsive layouts and styling elements. We can use calc(), max(), and min() math funct
8 min read
Explain various math functions that can be used in CoffeeScript
CoffeeScript is a lightweight language that compiles into JavaScript. As compared to JavaScript, it provides simple and easy-to-learn syntax avoiding the complex syntax of JavaScript. CoffeeScript is influenced by languages such as JavaScript, YAML, Ruby, Python and has also influenced languages that are LiveScript, MoonScript. Installation of Coff
4 min read
How to Differentiate Functions Using Math.js?
Differentiation is a fundamental concept in calculus, used extensively in mathematics, physics, engineering, and many other fields. While traditional methods of calculus involve manual calculations, modern computing tools like math.js can simplify this process significantly. Math.js is a powerful, open-source library for mathematics that runs in Ja
2 min read
How to Create Custom Functions in math.js?
Custom Functions in Math.js are nothing but user-defined functions that extend the capabilities of the library. They allow you to implement specific logic or calculations that are not available in the standard Math.js library. In this article, we will learn to create custom functions in Math.js to perform computations. You can install math.js using
2 min read
How to display values from database in real time without refreshing the webpage ?
Display values from database in real time without refreshing the webpage simply refers to auto fetch and re-rendering the data if there is any change due to user interaction or any update in the data from the APIs. In React JS, it can be done by using the states that re-render the UI on updates. PrerequisitesReact JSuseState HookuseEffect HookJavaS
3 min read
Difference between Regular functions and Arrow functions
This article discusses the major differences between regular functions and arrow functions. Arrow functions - a new feature introduced in ES6 - enable writing concise functions in JavaScript. While both regular and arrow functions work in a similar manner, there are certain interesting differences between them, as discussed below. Syntax: Regular f
2 min read
Javascript Program To Find Length Of The Longest Substring Without Repeating Characters
Given a string str, find the length of the longest substring without repeating characters.  For “ABDEFGABEF”, the longest substring are “BDEFGA” and "DEFGAB", with length 6.For “BBBB” the longest substring is “B”, with length 1.For "GEEKSFORGEEKS", there are two longest substrings shown in the below diagrams, with length 7 The desired time complexi
5 min read
JavaScript Math.round( ) function
JavaScript Math.round( ) function is used to round the number passed as a parameter to its nearest integer. Syntax: Math.round(value) Parameters: value: The number to be rounded to its nearest integer. Example 1: Rounding Off a number to its nearest integer To round off a number to its nearest integer, the math.round() function should be implemente
2 min read
JavaScript Math tan() Method
The Javas Math.tan() method in Javascript is used to return the tangent of a number. The Math. tan() method returns a numeric value that represents the tangent of the angle. The tan() is a static method of Math, therefore, it is always used as Math.tan(), rather than as a method of a Math object created. Syntax: Math.tan(value)Parameters: This meth
2 min read
JavaScript Math cbrt() Function
The Javascript Math.cbrt() function is used to find the cube root of a number. The cube root of a number is denoted as Math.cbrt(x)=y such that y^3=x. Syntax: Math.cbrt(number) Parameters: This function accepts a single parameter. number: The number whose cube root you want to know. Return Value: It returns the cube root of the given number. The be
1 min read
JavaScript Math acosh() Method
The Javascript Math.acosh() function in Javascript is used to return the hyperbolic arc-cosine of a number. Math.acosh (x) = arcosh(x) = y ≧ 0 such that cosh (y)=x acosh() is a static method of Math, therefore it is always used as Math.acosh(), rather than as a method of a Math object created. Syntax: Math.acosh(value) Parameters: This function acc
2 min read
JavaScript Math.clz32() Function
The Math.clz32( ) function in JavaScript returns the number of leading zero bits in the 32-bit binary representation of a number. Clz32 stands for Count Leading Zeros 32. If the passed parameter is not a number, then it will be converted into a number first and then converted to a 32-bit unsigned integer. If the converted 32-bit unsigned integer is
2 min read
JavaScript Math atan2() Method
Javascript Math.atan2() method is used to return the arctangent of the quotient of its arguments. The Math.atan2() method returns a numeric value between -Π and Π representing the angle theta of an (x, y) point and the positive x-axis. This is the counterclockwise angle, measured in radians, between the positive X-axis, and the point (x, y). Syntax
1 min read
JavaScript Math.asinh() Method
The Javascript Math.asinh() function is used to return the hyperbolic arc-sine of a number. The asinh() is a static method of Math, therefore it is always used as Math.asinh(), rather than as a method of a math object created. Syntax: Math.asinh(value) Parameter: This function accepts a single parameter value which is the number whose hyperbolic ar
2 min read
JavaScript Math exp() Method
JavaScript Math.exp() is used to get the value of ep, where p is any given number. The number e is a mathematical constant having an approximate value equal to 2.718. It was discovered by the Swiss mathematician Jacob Bernoulli. 2.718 is also called Euler's number. Syntax: Math.exp(p) Parameter: This method accepts a single parameter p: It can be a
2 min read
JavaScript Math.atanh() Function
The Javascript Math.atanh() function is an inbuilt function in JavaScript that is used to get the hyperbolic arctangent of a number. The hyperbolic arctangent is known by many names such as hyperbolic inverse tangent and atanh. It is the inverse of the hyperbolic tangent function i.e, The inverse hyperbolic tangent of any value says x is the value
2 min read
JavaScript Math SQRT2 Property
The Math.SQRT2 is a property in JavaScript which is simply used to find the value of the square root of 2, whose value is approximately 1.4142. That is, √ 2 = 1.4142 Syntax: Math.SQRT2; Return Value: It simply returns the value of the square root of 2, whose value is approximately 1.4142. Below is an example of the Math SQRT2 Property. Example 1: T
2 min read
JavaScript Math Reference
JavaScript Math object is used to perform mathematical operations on numbers. Math is an inbuilt object that works with numbers types but it does not work with BigInt. Example: Below example will give you a brief idea of JavaScript math objects. C/C++ Code // Return PI value(3.141592653589793) console.log(Math.PI); Output: This will print the value
4 min read
JavaScript Math.ceil( ) function
Math.ceil() function in JavaScript is used to round the number passed as a parameter to its nearest integer in an Upward direction of rounding i.e. towards the greater value. Syntax:Math.ceil(value);Parameters: This function accepts a single parameter as the number to be rounded to its nearest integer in the upward rounding method. Return Value: Re
2 min read
What is Math in JavaScript?
JavaScript provides a built-in Math object that provides a set of methods to perform mathematical operations. These operations range from basic to more complex functions like trigonometry and logarithms. The Math object allows you to perform calculations without the need for writing custom functions. Syntax:Math.methodName(argument1, argument2, ...
2 min read
What is the use of the Math.random Function in JavaScript ?
The Math.random() function in JavaScript is used to generate a pseudo-random floating-point number between 0 (inclusive) and 1 (exclusive). Example 1: Here, Math.random() is used to generate a random number. C/C++ Code let randomNumber = Math.random(); // Outputs a random number between 0 and 1 (excluding 1) console.log(randomNumber); Output0.76204
1 min read
What is the use of the Math.random() function in JavaScript ?
The Math.random() function is used to generate a pseudo-random floating-point number between 0 (inclusive) and 1 (exclusive). Syntax:Math.random();Example: Here, Math.random() generates a random number, which could be any floating-point value between 0 (inclusive) and 1 (exclusive). This random number is then stored in the variable randomNumber, an
1 min read
How to build a Math Expression Tokenizer using JavaScript ?
A math expression tokenizer is a fundamental component in parsing mathematical expressions. It breaks down a mathematical expression into smaller units called tokens, which are easier to process and evaluate. In JavaScript, building a math expression tokenizer can be achieved through various approaches, each with its advantages and considerations.
2 min read
Create a Math Sprint Game in HTML CSS & JavaScript
The math sprint game presents random math questions with four options. Players click the correct answer, and if correct, it's acknowledged; otherwise, an incorrect answer message is shown. A 20-second timer adds urgency, ending the game. The score (High Score) is displayed, along with buttons to start or restart the game. ApproachFirst, we make a f
5 min read