1
1
// Path: src/02-bigOnotation/01-big-o-intro.js
2
2
3
3
// O(1) - Constant Time
4
- function multiplyBy3 ( num ) {
5
- console . log ( `${ num } * 3 = ` , num * 3 ) ;
4
+ function secondsInDays ( numberOfDays ) {
5
+ if ( numberOfDays <= 0 || ! Number . isInteger ( numberOfDays ) ) {
6
+ throw new Error ( 'Invalid number of days' ) ;
7
+ }
8
+ return 60 * 60 * 24 * numberOfDays ;
6
9
}
7
10
8
11
console . log ( 'O(1) - Constant Time' ) ;
9
- multiplyBy3 ( 5 ) ;
10
- multiplyBy3 ( 50 ) ;
12
+ console . log ( 'Seconds in 1 day: ' , secondsInDays ( 1 ) ) ; // 86400
13
+ console . log ( 'Seconds in 10 days: ' , secondsInDays ( 10 ) ) ; // 864000
14
+ console . log ( 'Seconds in 100 days: ' , secondsInDays ( 100 ) ) ; // 8640000
11
15
12
16
// O(n) - Linear Time
13
- function multiplicationTable ( num , x ) {
14
- for ( let i = 1 ; i <= x ; i ++ ) {
15
- console . log ( `${ num } * ${ i } = ` , num * i ) ;
17
+ function calculateTotalExpenses ( monthlyExpenses ) {
18
+ let total = 0 ;
19
+ for ( let i = 0 ; i < monthlyExpenses . length ; i ++ ) {
20
+ total += monthlyExpenses [ i ] ;
16
21
}
22
+ return total ;
17
23
}
18
24
19
25
console . log ( '*******************' ) ;
20
26
console . log ( 'O(n) - Linear Time' ) ;
21
- console . log ( 'Multiplication table for 5 with x = 3' ) ;
22
- multiplicationTable ( 5 , 3 ) ;
23
-
24
- console . log ( 'Multiplication table for 5 with x = 10' ) ;
25
- multiplicationTable ( 5 , 10 ) ;
27
+ console . log ( 'January: ' , calculateTotalExpenses ( [ 100 , 200 , 300 ] ) ) ; // 600
28
+ console . log ( 'February: ' , calculateTotalExpenses ( [ 200 , 300 , 400 ] ) ) ; // 900
29
+ console . log ( 'March: ' , calculateTotalExpenses ( [ 30 , 40 , 50 , 100 , 50 ] ) ) ; // 270
26
30
27
31
// O(n^2) - Quadratic Time
28
- function multiplicationTable2 ( num , x ) {
29
- for ( let i = 1 ; i <= num ; i ++ ) {
30
- console . log ( `Multiplication table for ${ i } with x = ${ x } ` ) ;
31
- for ( let j = 1 ; j <= x ; j ++ ) {
32
- console . log ( ` ${ i } * ${ j } = ` , i * j ) ;
32
+ function calculateExpensesMatrix ( monthlyExpenses ) {
33
+ let total = 0 ;
34
+ for ( let i = 0 ; i < monthlyExpenses . length ; i ++ ) {
35
+ for ( let j = 0 ; j < monthlyExpenses [ i ] . length ; j ++ ) {
36
+ total += monthlyExpenses [ i ] [ j ] ;
33
37
}
34
38
}
39
+ return total ;
35
40
}
36
41
37
42
console . log ( '************************' ) ;
38
43
console . log ( 'O(n^2) - Quadratic Time' ) ;
39
- multiplicationTable2 ( 3 , 2 ) ;
40
-
41
- // O(nˆ3) - Cubic Time
42
- function multiplicationTable3 ( num , x ) {
43
- for ( let i = 1 ; i <= num ; i ++ ) {
44
- for ( let j = 1 ; j <= x ; j ++ ) {
45
- for ( let k = 1 ; k <= x ; k ++ ) {
46
- console . log ( `${ i } * ${ j } * ${ k } = ` , i * j * k ) ;
47
- }
48
- }
49
- }
50
- }
51
-
52
- console . log ( '************************' ) ;
53
- console . log ( 'O(n^3) - Cubic Time' ) ;
54
- multiplicationTable3 ( 2 , 3 ) ;
55
-
56
- // calculating the time complexity of the function
57
- function multiplicationTableRefactored ( num , x ) {
44
+ const monthlyExpenses = [
45
+ [ 100 , 105 , 100 , 115 , 120 , 135 ] ,
46
+ [ 180 , 185 , 185 , 185 , 200 , 210 ] ,
47
+ [ 30 , 30 , 30 , 30 , 30 , 30 ] ,
48
+ [ 2000 , 2000 , 2000 , 2000 , 2000 , 2000 ] ,
49
+ [ 600 , 620 , 610 , 600 , 620 , 600 ] ,
50
+ [ 150 , 100 , 130 , 200 , 150 , 100 ]
51
+ ] ;
52
+ console . log ( 'Total expenses: ' , calculateExpensesMatrix ( monthlyExpenses ) ) ; // 18480
58
53
59
- let s = '' ; // {1}
60
- let numberOfAsterisks = num * x ; // {2}
61
- for ( let i = 1 ; i <= numberOfAsterisks ; i ++ ) { // {3}
54
+ // calculating the time complexity of the function calculateExpensesMatrix
55
+ function multiplicationTable ( num , x ) {
56
+ let s = '' ;
57
+ let numberOfAsterisks = num * x ;
58
+ for ( let i = 1 ; i <= numberOfAsterisks ; i ++ ) {
62
59
s += '*' ;
63
60
}
64
- console . log ( s ) ; // {4}
65
- console . log ( 'Calculating the time complexity of a function' ) ; // {5}
61
+ console . log ( s ) ;
66
62
67
- for ( let i = 1 ; i <= num ; i ++ ) { // {6}
68
- console . log ( `Multiplication table for ${ i } with x = ${ x } ` ) ; // {7}
69
- for ( let j = 1 ; j <= x ; j ++ ) { // {8}
70
- console . log ( `${ i } * ${ j } = ` , i * j ) ; // {9}
63
+ for ( let i = 1 ; i <= num ; i ++ ) {
64
+ console . log ( `Multiplication table for ${ i } with x = ${ x } ` ) ;
65
+ for ( let j = 1 ; j <= x ; j ++ ) {
66
+ console . log ( `${ i } * ${ j } = ` , i * j ) ;
71
67
}
72
68
}
73
69
}
74
70
75
- multiplicationTableRefactored ( 3 , 2 ) ;
71
+
76
72
// to see the output of this file use the command: node src/02-bigOnotation/01-big-o-intro.js
0 commit comments