Skip to content

Commit 582b68d

Browse files
committed
chapter 2: changes after feedback
1 parent 446bbab commit 582b68d

File tree

2 files changed

+99
-46
lines changed

2 files changed

+99
-46
lines changed

src/02-bigOnotation/01-big-o-intro.js

Lines changed: 42 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -1,76 +1,72 @@
11
// Path: src/02-bigOnotation/01-big-o-intro.js
22

33
// 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;
69
}
710

811
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
1115

1216
// 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];
1621
}
22+
return total;
1723
}
1824

1925
console.log('*******************');
2026
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
2630

2731
// 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];
3337
}
3438
}
39+
return total;
3540
}
3641

3742
console.log('************************');
3843
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
5853

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++) {
6259
s += '*';
6360
}
64-
console.log(s); // {4}
65-
console.log('Calculating the time complexity of a function'); // {5}
61+
console.log(s);
6662

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);
7167
}
7268
}
7369
}
7470

75-
multiplicationTableRefactored(3, 2);
71+
7672
// to see the output of this file use the command: node src/02-bigOnotation/01-big-o-intro.js

src/02-bigOnotation/03-exercises.js

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
// Path: src/02-bigOnotation/03-exercises.js
2+
3+
/* What is the time and space complexities for each of the following functions.
4+
Try them with different inputs to explore how they behave with different inputs */
5+
6+
// time complexity: O(1) - Constant Time
7+
// space complexity: O(1) - Constant Space
8+
const oddOrEven = (array) => array.length % 2 === 0 ? 'even' : 'odd';
9+
10+
console.log(oddOrEven([1, 2, 3, 4, 5])); // odd
11+
console.log(oddOrEven([1, 2, 3, 4, 5, 6])); // even
12+
13+
// time complexity: O(n) - Linear Time
14+
// space complexity: O(1) - Constant Space
15+
function calculateAverage(array) {
16+
let sum = 0;
17+
for (let i = 0; i < array.length; i++) {
18+
sum += array[i];
19+
}
20+
return sum / array.length;
21+
}
22+
23+
console.log(calculateAverage([1, 2, 3, 4, 5])); // 3
24+
console.log(calculateAverage([1, 2, 3, 4, 5, 6])); // 3.5
25+
26+
// time complexity: O(n^2) - Quadratic Time
27+
// space complexity: O(1) - Constant Space
28+
function hasCommonElements(array1, array2) {
29+
for (let i = 0; i < array1.length; i++) {
30+
for (let j = 0; j < array2.length; j++) {
31+
if (array1[i] === array2[j]) {
32+
return true;
33+
}
34+
}
35+
}
36+
return false;
37+
}
38+
39+
console.log(hasCommonElements([1, 2, 3, 4, 5], [6, 7, 8, 9, 10])); // false
40+
console.log(hasCommonElements([1, 2, 3, 4, 5], [5, 6, 7, 8, 9])); // true
41+
42+
// time complexity: O(n) - Linear Time
43+
// space complexity: O(n) - Linear Space
44+
function getOddNumbers(array) {
45+
const result = [];
46+
for (let i = 0; i < array.length; i++) {
47+
if (array[i] % 2 !== 0) {
48+
result.push(array[i]);
49+
}
50+
}
51+
return result;
52+
}
53+
54+
console.log(getOddNumbers([1, 2, 3, 4, 5])); // [1, 3, 5]
55+
console.log(getOddNumbers([1, 2, 3, 4, 5, 6])); // [1, 3, 5]
56+
57+
// to see the output of this file use the command: node src/02-bigOnotation/03-exercises.js

0 commit comments

Comments
 (0)