-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBiggest_Number.js
More file actions
32 lines (24 loc) · 1.02 KB
/
Biggest_Number.js
File metadata and controls
32 lines (24 loc) · 1.02 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
// ---------------------------Description---------------------------------------
// The task is to create a function called biggestNumber that takes an array of numbers as input and returns the largest number in the array.
// -----------------------------------------------------------------------------
//Solution 1 using (ES6)
const biggestNumber01 = (arr) => {
return Math.max(...arr)
}
console.log('result_01', biggestNumber01([19,1,0,20,2,105,-255,30,106])) //result ==> 106
//solution 2 using js
const biggestNumber02 = (arr) => {
// Initialize max to the first element of the array
let max = arr[0];
// Loop through the array starting from the second element
for (let index = 1; index < arr.length; index++) {
// Compare each element to the current max
if (arr[index] > max) {
// If the current element is greater, update max
max = arr[index];
}
}
// Return the maximum value found
return max;
};
console.log('result_02', biggestNumber02([19,1,0,20,2,105,-255,30,106])) //result ==> 106