-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathJavaScriptChallenges.js
More file actions
344 lines (248 loc) · 8.3 KB
/
JavaScriptChallenges.js
File metadata and controls
344 lines (248 loc) · 8.3 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
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
/*
Question 1:
Write a JavaScript function called reverseString that takes a string as input and returns the reversed version of the string. The function should reverse the order of characters in the string.
Example:
Input: "hello"
Output: "olleh"
Input: "world"
Output: "dlrow"
*/
function reverseString(strInput) {
const reversed = strInput.split('').reverse().join('');
return reversed;
}
//console.log(reverseString("Hello"));
//console.log(reverseString("World"));
/*
Question 2:
Write a JavaScript function called sumArray that takes an array of numbers as input and returns the sum of all the numbers in the array.
Example:
Input: [1, 2, 3, 4, 5]
Output: 15
Input: [10, -5, 7, 12]
Output: 24
*/
var arrNumbers = [1, 2, 3, 4, 5];
function sumArray(angArrNumbers) {
var sum = 0;
angArrNumbers.forEach(element => {
sum += element;
});
return sum;
}
//console.log(sumArray(arrNumbers));
/*
Question 5:
Write a JavaScript function called removeDuplicates that takes an array as input and returns a new array with duplicate elements removed. The order of the elements in the resulting array should be preserved.
Example:
Input: [1, 2, 3, 2, 4, 1, 5]
Output: [1, 2, 3, 4, 5]
Input: [5, 7, 5, 9, 2, 7, 9]
Output: [5, 7, 9, 2]
*/
var arrInput = [1, 2, 3, 2, 4, 1, 5];
function removeDuplicates(arrInput) {
function isUnique(value, index, array) {
return array.indexOf(value) === index;
}
//usage example:
var uniqueArray = arrInput.filter(isUnique);
return uniqueArray;
}
//console.log(removeDuplicates(arrInput));
/*
Question 6:
Question:
Write a JavaScript function called findMax that takes an array of numbers as input and returns the maximum value in the array. The function should handle both positive and negative numbers.
Example:
Input: [1, 4, -2, 7, 0]
Output: 7
Input: [-10, -5, -3, -7]
Output: -3
*/
const numInputArraynumInputArray = [-10, -5, -3, -7];
function findMax(numInputArray){
return Math.max(...numInputArray);
}
//console.log(findMax(numInputArraynumInputArray));
/*
Question 1:
Write a JavaScript function called isPalindrome that takes a string as input and returns true if the string is a palindrome (reads the same forwards and backwards), and false otherwise.
Example:
Input: "racecar"
Output: true
Input: "hello"
Output: false
*/
function isPalindrome(strInput) {
const reversed = strInput.split('').reverse().join('');
if(strInput === reversed)
return true;
else
return false;
}
//console.log(isPalindrome("hello"));
/*
Question 2:
Write a JavaScript function called capitalizeWords that takes a sentence as input and returns a new sentence where the first letter of each word is capitalized. Assume that the input sentence consists of words separated by spaces.
Example:
Input: "hello world"
Output: "Hello World"
Input: "javascript is awesome"
Output: "Javascript Is Awesome"
*/
function capitalizeWords(strInput)
{
var strCapitalizeWords = "";
function toCapitalizeFirstLetter(strWord)
{
return strWord[0].toUpperCase();
}
const elementstrCapitalizedWords = strInput.split(' ');
elementstrCapitalizedWords.forEach(element => {
strCapitalizeWords += toCapitalizeFirstLetter(element) + element.slice(1) + " "
});
return strCapitalizeWords.trim();
}
//console.log(capitalizeWords("hello world"));
/*
Question 3:
Write a JavaScript function called flattenArray that takes a nested array as input and returns a new array with all the elements flattened into a single-level array. The function should handle arrays of any depth.
Example:
Input: [1, [2, [3, [4]], 5]]
Output: [1, 2, 3, 4, 5]
Input: [6, [7, 8], [9, [10, 11, [12]]]]
Output: [6, 7, 8, 9, 10, 11, 12]
*/
//const angArr = [1, [2, [3, [4]], 5]];
function flattenArray(arrInputArray){
return arrInputArray.flat(Infinity);
}
//console.log(flattenArray(angArr));
/**
Question 4:
Write a JavaScript function called findDuplicates that takes an array as input and returns an array containing all the duplicate elements from the input array. The order of elements in the resulting array should be the same as in the input array.
Example:
Input: [1, 2, 3, 2, 4, 1, 5]
Output: [1, 2]
Input: [5, 7, 5, 9, 2, 7, 9]
Output: [5, 7, 9]
*/
const angArr2 = [1, 2, 3, 2, 4, 1, 5];
function findDuplicates(inputArray)
{
const duplicateArr = [];
for (const intArrIndex in inputArray)
{
for (const intArrIndex2 in inputArray)
{
if (intArrIndex != intArrIndex2)
{
if (inputArray[intArrIndex] == inputArray[intArrIndex2])
{
if (duplicateArr.includes(inputArray[intArrIndex]) === false)
{
duplicateArr.push(inputArray[intArrIndex]);
}
}
}
}
}
return duplicateArr;
}
//console.log(findDuplicates(angArr2));
/*
Question 5:
Write a JavaScript function called removeFalsyValues that takes an array as input and returns a new array with all falsy values removed. Falsy values include false, 0, "" (empty string), null, undefined, and NaN. The order of the remaining elements should be preserved.
Example:
Input: [1, null, "hello", 0, false, undefined, 42, NaN, ""]
Output: [1, "hello", 42]
Input: [false, "apple", null, 25, "", "orange", undefined]
Output: ["apple", 25, "orange"]
*/
const angArr5 = [false, "apple", null, 25, "", "orange", undefined];
function removeFalsyValues(inputArray)
{
const cleanArray = [];
inputArray.filter(element => {
//console.log("element : " + element + " [type : " + typeof(element) + "]");
switch(typeof(element)) {
case "number":
//console.log("element: " + element + " [type: number]");
if(element > 0)
{
cleanArray.push(element);
}
break;
case "string":
//console.log("element: " + element + " [type: string]");
if(element.length > 0)
{
cleanArray.push(element);
}
break;
case "boolean":
//console.log("element: " + element + " [type: boolean]");
if(element === true)
{
cleanArray.push(element);
}
break;
}
});
return cleanArray;
}
//console.log(removeFalsyValues(angArr5));
/**
*
* Question 6:
Write a JavaScript function called calculateAverage that takes an array of numbers as input and returns the average (mean) value of the numbers. Round the result to two decimal places.
Example:
Input: [5, 10, 15, 20]
Output: 12.50
Input: [1, 3, 5, 7, 9]
Output: 5.00
*/
function calculateAverage(inputArray)
{
var numTotal = 0;
inputArray.forEach(function(item, index){
numTotal += item;
});
return (numTotal / inputArray.length).toFixed(2);
}
const angArr6 = [1, 3, 5, 7, 9];
//console.log(calculateAverage(angArr6));
/**
*
* Question 7:
Write a JavaScript function called removeDuplicatesCaseInsensitive that takes an array of strings as input and returns a new array with all the duplicate strings removed. The comparison should be case-insensitive, meaning that strings with the same characters in different cases should be considered duplicates and only one occurrence should be included in the resulting array. The order of the remaining strings should be preserved.
Example:
Input: ["apple", "Orange", "banana", "orange", "APPLE"]
Output: ["apple", "Orange", "banana"]
Input: ["cat", "DOG", "cat", "dog", "Cat"]
Output: ["cat", "DOG"]
*
*/
function removeDuplicatesCaseInsensitive(inputArray)
{
var arrTempFiltered = [];
inputArray.forEach((element) => {
var arrFilteredLowered = [];
if (arrTempFiltered.length > 0)
{
arrFilteredLowered = arrTempFiltered.map(element => element.toLowerCase());
if (arrFilteredLowered.indexOf(element.toLocaleLowerCase()) === -1)
{
arrTempFiltered.push(element);
}
}
else
{
arrTempFiltered.push(element);
}
});
return arrTempFiltered;
}
const arr7 = ["cat", "DOG", "cat", "dog", "Cat"];
console.log(removeDuplicatesCaseInsensitive(arr7));