JS Coding Questions Logo
JS Coding Questions
#247💼 Interview💻 Code

How do you find the min and max values without Math functions

Advertisement

728x90

You can write functions which loop through an array comparing each value with the lowest value or highest value to find the min and max values. Let's create those functions to find min and max values,

javascript
1var marks = [50, 20, 70, 60, 45, 30];
2
3function findMin(arr) {
4  var length = arr.length;
5  var min = Infinity;
6  while (length--) {
7  if (arr[length] < min) {
8  min = arr[length];
9  }
10  }
11  return min;
12}
13
14function findMax(arr) {
15  var length = arr.length;
16  var max = -Infinity;
17  while (length--) {
18  if (arr[length] > max) {
19  max = arr[length];
20  }
21  }
22  return max;
23}
24
25console.log(findMin(marks));
26
27console.log(findMax(marks));

Advertisement

Responsive Ad
🎯 Practice NowRelated Challenge

JavaScript Coding Exercise 76

Test your knowledge with this interactive coding challenge.

Start Coding

Advertisement

728x90
247of476
How do you find the min and max values without Math functions | JSCodingQuestions.com