forked from web-dev-open/lab-js-basic-algorithms
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathnew.js
More file actions
228 lines (190 loc) · 6.02 KB
/
new.js
File metadata and controls
228 lines (190 loc) · 6.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
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
//Iteration 1
function maxOfTwoNumbers(num1, num2) {
if (num1 > num2) {
return num1;
} else {
return num2;
}
}
// Example usage:
//const result = maxOfTwoNumbers(5, 10);
//console.log("The largest number is:", result); // Output: 10
// Example usage:
result = maxOfTwoNumbers(5, 10)
console.log("The largest number is:", result)
// Iteration 2
function findLongestWord(words) {
if (words.length === 0) {
return null; // Return null if the array is empty
}
let longestWord = words[0]; // Assume the first word is the longest
for (let i = 1; i < words.length; i++) {
if (words[i].length > longestWord.length) {
longestWord = words[i]; // Update longestWord if a longer word is found
}
}
return longestWord;
}
// Test the function
words = ['mystery', 'brother', 'aviator', 'crocodile', 'pearl', 'orchard', 'crackpot'];
const longestWord = findLongestWord(words);
console.log("The longest word is:", longestWord); // Output: "crocodile"
//Iteration 3
function sumNumbers(nums) {
let sum = 0; // Initialize sum variable to store the total sum
// Iterate over each number in the array and add it to the sum
for (let i = 0; i < nums.length; i++) {
sum += nums[i];
}
return sum;
}
// Test the function
numbers = [6, 12, 1, 18, 13, 16, 2, 1, 8, 10];
let totalSum = sumNumbers(numbers);
console.log("The sum of all numbers is:", totalSum); // Output: 87
//Iteration 3.1
function sum(arr) {
let total = 0;
for (let element of arr) {
if (typeof element === 'number' || typeof element === 'boolean') {
total += element; // For numbers and booleans, add directly to total
} else if (typeof element === 'string') {
total += element.length; // For strings, add their length to total
}
// Ignore other types of data
}
return total;
}
// Test the function
mixedArr = [6, 12, 'miami', 1, true, 'barca', '200', 'lisboa', 8, 10];
const result = sum(mixedArr);
console.log("The sum is:", result); // Output: 57
// Iteration 4 level 1
function averageNumbers(numbers) {
var sum = 0;
for (var i = 0; i < numbers.length; i++) {
sum += numbers[i];
}
var avg = sum / numbers.length;
return avg;
}
var numbers = [2, 6, 9, 10, 7, 4, 1, 9];
console.log(averageNumbers(numbers));
// Iteration 4 level 2
function averageWordLength(words) {
if (words.length === 0) return 0;
var totalLength = 0;
for (var i = 0; i < words.length; i++) {
totalLength += words[i].length;
}
return totalLength / words.length;
}
words = ['seat', 'correspond', 'linen', 'motif', 'hole', 'smell', 'smart', 'chaos', 'fuel', 'palace'];
console.log(averageWordLength(words));
// Iteration 4.1
function avg(arr) {
var sum = 0;
var count = 0;
for (var i = 0; i < arr.length; i++) {
var num;
if (typeof arr[i] === 'number') {
num = arr[i];
} else if (typeof arr[i] === 'boolean') {
num = arr[i] ? 1 : 0;
} else if (typeof arr[i] === 'string') {
num = arr[i].length;
} else {
continue;
}
sum += num;
count++;
}
return count ? sum / count : 0;
}
const mixedArr = [6, 12, 'miami', 1, true, 'barca', '200', 'lisboa', 8, 10];
console.log(avg(mixedArr));
// Iteration 5
function uniquifyArray(words) {
var uniqueArray = [];
for (var i = 0; i < words.length; i++) {
if (uniqueArray.indexOf(words[i]) === -1) {
uniqueArray.push(words[i]);
}
}
return uniqueArray;
}
words = [
'crab', 'poison', 'contagious', 'simple', 'bring',
'sharp', 'playground', 'poison', 'communion', 'simple', 'bring'
];
console.log(uniquifyArray(words));
// Iteration 6
function doesWordExist(words, word) {
for (var i = 0; i < words.length; i++) {
if (words[i] === word) {
return true;
}
}
return false;
}
words = ['machine', 'subset', 'trouble', 'starting', 'matter', 'eating', 'truth', 'disobedience'];
console.log(doesWordExist(words, 'subset'));
console.log(doesWordExist(words, 'apple'));
// Iteration 7
function howManyTimes(words, word) {
var count = 0;
for (var i = 0; i < words.length; i++) {
if (words[i] === word) {
count++; // Increment count each time the word is found
}
}
return count;
}
const words = [
'machine', 'matter', 'subset', 'trouble', 'starting',
'matter', 'eating', 'matter', 'truth', 'disobedience', 'matter'
];
console.log(howManyTimes(words, 'matter'));
//Iteration 8
function greatestProduct(matrix) {
const rows = matrix.length;
const cols = matrix[0].length;
let maxProduct = 0;
// Check horizontal products
for (let i = 0; i < rows; i++) {
for (let j = 0; j <= cols - 4; j++) {
const product = matrix[i][j] * matrix[i][j + 1] * matrix[i][j + 2] * matrix[i][j + 3];
if (product > maxProduct) {
maxProduct = product;
}
}
}
// Check vertical products
for (let i = 0; i <= rows - 4; i++) {
for (let j = 0; j < cols; j++) {
const product = matrix[i][j] * matrix[i + 1][j] * matrix[i + 2][j] * matrix[i + 3][j];
if (product > maxProduct) {
maxProduct = product;
}
}
}
// Check diagonal (right-down) products
for (let i = 0; i <= rows - 4; i++) {
for (let j = 0; j <= cols - 4; j++) {
const product = matrix[i][j] * matrix[i + 1][j + 1] * matrix[i + 2][j + 2] * matrix[i + 3][j + 3];
if (product > maxProduct) {
maxProduct = product;
}
}
}
// Check diagonal (left-down) products
for (let i = 0; i <= rows - 4; i++) {
for (let j = 3; j < cols; j++) {
const product = matrix[i][j] * matrix[i + 1][j - 1] * matrix[i + 2][j - 2] * matrix[i + 3][j - 3];
if (product > maxProduct) {
maxProduct = product;
}
}
}
return maxProduct;
}