-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
48 lines (34 loc) · 1007 Bytes
/
Copy pathindex.js
File metadata and controls
48 lines (34 loc) · 1007 Bytes
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
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
// write two algoritms :
// 1.The first algorithm should take an array of numbers as input
// and return the minimium value in the array (i.e the smallest number)
function findMinimumValue(numbers){
return Math.min(...numbers); //-1
}
console.log(findMinimumValue([1, 2, 3, 4]));
//O(n) - Depends on the number of n times.
//Best case : [1, 2, 3]
//Worst case : [3, 2, 1]
//Average case: [2, 1, 3]
function findMinimumValue(numbers) {
return numbers.reduce((a, b) => Math.min(a, b));
}
// 2. The second algorithm should take a number as input and return true
// if it's an evennummber, false for odd numbers.
function isEven(n){
if(n % 2 === 0){
return "even"
}
else {
return "odd"
}
}
console.log(isEven(10)); // Output: "even"
//Time complexity
function isEven(num) {
//The first line executes only once
return num % 2 === 0;
}
//This is constant time
//Why? we have no if statements or loops
//One Case: 0(1)
console.log(isEven(10));