Skip to content

Commit 8893233

Browse files
committed
feat:added the day3 assignment
1 parent c47a75e commit 8893233

File tree

2 files changed

+305
-0
lines changed

2 files changed

+305
-0
lines changed

day3.js

Whitespace-only changes.

day3.txt

Lines changed: 305 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,305 @@
1+
1.Do the below programs in anonymous function & IIFE
2+
3+
#anonymous :
4+
5+
a.Print odd numbers in an array
6+
7+
var printOddNumbers = function(arr) {
8+
arr.forEach(function(num) {
9+
if (num % 2 !== 0) {
10+
console.log(num);
11+
}
12+
});
13+
};
14+
15+
var numbers = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10];
16+
printOddNumbers(numbers);
17+
18+
b.Convert all the strings to title caps in a string array
19+
20+
var convertToTitleCase = function(arr) {
21+
return arr.map(function(str) {
22+
return str.toLowerCase().replace(/\b\w/g, function(char) {
23+
return char.toUpperCase();
24+
});
25+
});
26+
};
27+
28+
var strings = ["hello world", "good morning", "have a nice day"];
29+
var titleCaseStrings = convertToTitleCase(strings);
30+
console.log(titleCaseStrings);
31+
32+
C.Sum of all numbers in an array
33+
34+
var sumOfArray = function(arr) {
35+
return arr.reduce(function(total, num) {
36+
return total + num;
37+
}, 0);
38+
};
39+
40+
var numbers = [1, 2, 3, 4, 5];
41+
var sum = sumOfArray(numbers);
42+
console.log("The sum of all numbers in the array is:", sum);
43+
44+
d.Return all the prime numbers in an array
45+
var isPrime = function(num) {
46+
if (num <= 1) {
47+
return false;
48+
}
49+
for (var i = 2; i <= Math.sqrt(num); i++) {
50+
if (num % i === 0) {
51+
return false;
52+
}
53+
}
54+
return true;
55+
};
56+
57+
var findPrimeNumbers = function(arr) {
58+
return arr.filter(function(num) {
59+
return isPrime(num);
60+
});
61+
};
62+
63+
var numbers = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10];
64+
var primeNumbers = findPrimeNumbers(numbers);
65+
console.log("Prime numbers in the array:", primeNumbers);
66+
67+
e.Return all the palindromes in an array
68+
var isPalindrome = function(str) {
69+
var reversedStr = str.split('').reverse().join('');
70+
return str === reversedStr;
71+
};
72+
73+
var findPalindromes = function(arr) {
74+
return arr.filter(function(word) {
75+
return isPalindrome(word);
76+
});
77+
};
78+
79+
var words = ["radar", "level", "hello", "world", "madam"];
80+
var palindromes = findPalindromes(words);
81+
console.log("Palindromes in the array:", palindromes);
82+
83+
84+
f.Return median of two sorted arrays of the same size.
85+
86+
var findMedianSortedArrays = function(nums1, nums2) {
87+
var mergedArray = mergeArrays(nums1, nums2);
88+
var n = mergedArray.length;
89+
90+
if (n % 2 === 0) {
91+
// If the merged array length is even, return the average of the middle two elements
92+
var midIndex = n / 2;
93+
return (mergedArray[midIndex - 1] + mergedArray[midIndex]) / 2;
94+
} else {
95+
// If the merged array length is odd, return the middle element
96+
var midIndex = Math.floor(n / 2);
97+
return mergedArray[midIndex];
98+
}
99+
};
100+
101+
var mergeArrays = function(nums1, nums2) {
102+
var merged = [];
103+
var i = 0, j = 0;
104+
105+
while (i < nums1.length && j < nums2.length) {
106+
if (nums1[i] < nums2[j]) {
107+
merged.push(nums1[i]);
108+
i++;
109+
} else {
110+
merged.push(nums2[j]);
111+
j++;
112+
}
113+
}
114+
115+
while (i < nums1.length) {
116+
merged.push(nums1[i]);
117+
i++;
118+
}
119+
120+
while (j < nums2.length) {
121+
merged.push(nums2[j]);
122+
j++;
123+
}
124+
125+
return merged;
126+
};
127+
128+
var nums1 = [1, 3, 5];
129+
var nums2 = [2, 4, 6];
130+
var median = findMedianSortedArrays(nums1, nums2);
131+
console.log("Median of the two sorted arrays:", median);
132+
133+
g.Remove duplicates from an array
134+
135+
var removeDuplicates = function(arr) {
136+
return [...new Set(arr)];
137+
};
138+
139+
var array = [1, 2, 3, 4, 2, 3, 5];
140+
var uniqueArray = removeDuplicates(array);
141+
console.log("Array with duplicates removed:", uniqueArray);
142+
143+
144+
h.Rotate an array by k times
145+
146+
var rotateArray = function(arr, k) {
147+
k = k % arr.length;
148+
reverseArray(arr, 0, arr.length - 1);
149+
reverseArray(arr, 0, k - 1);
150+
reverseArray(arr, k, arr.length - 1);
151+
return arr;
152+
};
153+
154+
var reverseArray = function(arr, start, end) {
155+
while (start < end) {
156+
var temp = arr[start];
157+
arr[start] = arr[end];
158+
arr[end] = temp;
159+
start++;
160+
end--;
161+
}
162+
};
163+
164+
var array = [1, 2, 3, 4, 5];
165+
var k = 2;
166+
var rotatedArray = rotateArray(array, k);
167+
console.log("Array after rotating", k, "times:", rotatedArray);
168+
169+
#IIFE:
170+
a.Print odd numbers in an array
171+
(function(arr){
172+
for(let num of arr){
173+
if(num % 2 !== 0){
174+
console.log(num);
175+
}
176+
}
177+
})([1, 2, 3, 4, 5]);
178+
179+
b.Convert all the strings to title caps in a string array
180+
(function(arr){
181+
for(let i = 0; i < arr.length; i++){
182+
arr[i] = arr[i].charAt(0).toUpperCase() + arr[i].slice(1).toLowerCase();
183+
}
184+
console.log(arr);
185+
})(["hello", "world", "this", "is", "a", "test"]);
186+
187+
c.Sum of all numbers in an array
188+
(function(arr){
189+
let sum = 0;
190+
for(let num of arr){
191+
sum += num;
192+
}
193+
console.log(sum);
194+
})([1, 2, 3, 4, 5]);
195+
196+
d.Return all the prime numbers in an array
197+
(function(arr){
198+
function isPrime(num) {
199+
if (num <= 1) return false;
200+
if (num <= 3) return true;
201+
202+
if (num % 2 === 0 || num % 3 === 0) return false;
203+
204+
let i = 5;
205+
while (i * i <= num) {
206+
if (num % i === 0 || num % (i + 2) === 0) return false;
207+
i += 6;
208+
}
209+
210+
return true;
211+
}
212+
213+
let primes = arr.filter(num => isPrime(num));
214+
console.log(primes);
215+
})([1, 2, 3, 4, 5, 6, 7, 8, 9, 10]);
216+
217+
e.Return all the palindromes in an array
218+
(function(arr){
219+
function isPalindrome(str) {
220+
return str === str.split('').reverse().join('');
221+
}
222+
223+
let palindromes = arr.filter(word => isPalindrome(word));
224+
console.log(palindromes);
225+
})(["radar", "hello", "level", "world", "deified"]);
226+
227+
f.Return median of two sorted arrays of the same size.
228+
(function(arr1, arr2){
229+
let mergedArr = arr1.concat(arr2);
230+
mergedArr.sort((a, b) => a - b);
231+
232+
let length = mergedArr.length;
233+
let median;
234+
if(length % 2 === 0){
235+
median = (mergedArr[length / 2 - 1] + mergedArr[length / 2]) / 2;
236+
} else {
237+
median = mergedArr[Math.floor(length / 2)];
238+
}
239+
240+
console.log(median);
241+
})([1, 3, 5], [2, 4, 6]);
242+
243+
g.Remove duplicates from an array
244+
(function(arr){
245+
let uniqueArr = [...new Set(arr)];
246+
console.log(uniqueArr);
247+
})([1, 2, 2, 3, 4, 4, 5]);
248+
249+
h.Rotate an array by k times
250+
(function(arr, k){
251+
for(let i = 0; i < k; i++){
252+
arr.unshift(arr.pop());
253+
}
254+
console.log(arr);
255+
})([1, 2, 3, 4, 5], 2);
256+
257+
258+
2.Do the below programs in arrow functions.
259+
260+
a.Print odd numbers in an array
261+
const printOddNumbers = (arr) => {
262+
arr.forEach(num => {
263+
if (num % 2 !== 0) {
264+
console.log(num);
265+
}
266+
});
267+
}
268+
269+
b.Convert all the strings to title caps in a string array
270+
const titleCaps = (arr) => {
271+
return arr.map(str => str.charAt(0).toUpperCase() + str.slice(1).toLowerCase());
272+
}
273+
274+
c.Sum of all numbers in an array
275+
const sumOfArray = (arr) => {
276+
return arr.reduce((acc, curr) => acc + curr, 0);
277+
}
278+
279+
d.Return all the prime numbers in an array
280+
const isPrime = (num) => {
281+
if (num <= 1) return false;
282+
if (num <= 3) return true;
283+
if (num % 2 === 0 || num % 3 === 0) return false;
284+
285+
let i = 5;
286+
while (i * i <= num) {
287+
if (num % i === 0 || num % (i + 2) === 0) return false;
288+
i += 6;
289+
}
290+
return true;
291+
}
292+
293+
const getPrimes = (arr) => {
294+
return arr.filter(num => isPrime(num));
295+
}
296+
297+
e.Return all the palindromes in an array
298+
const isPalindrome = (str) => {
299+
const reversed = str.split('').reverse().join('');
300+
return str === reversed;
301+
}
302+
303+
const getPalindromes = (arr) => {
304+
return arr.filter(str => isPalindrome(str));
305+
}

0 commit comments

Comments
 (0)