-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathjavascript.js
More file actions
204 lines (169 loc) · 5.45 KB
/
Copy pathjavascript.js
File metadata and controls
204 lines (169 loc) · 5.45 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
//1. Do the below programs in anonymous function & IIFE
//a. Print odd numbers in an array ?
const numbers = [1, 2, 3, 4, 5, 6, 7, 8, 9];
const oddNumbers = numbers.filter(function (num) {
return num % 2 !== 0;
});
console.log("Odd Numbers:", oddNumbers);
//b. Convert all the strings to title caps in a string array ?
const stringArray = ["hello world", "javascript is awesome", "web development"];
const titleCaseArray = stringArray.map(function (str) {
return str
.split(" ")
.map(function (word) {
return word.charAt(0).toUpperCase() + word.slice(1).toLowerCase();
})
.join(" ");
});
console.log("Title Case Strings:", titleCaseArray);
//c. Sum of all numbers in an array ?
function calculateSum(numbers) {
let sum = 0;
for (let i = 0; i < numbers.length; i++) {
sum += numbers[i];
}
return sum;
}
const numbers = [1, 2, 3, 4, 5, 6, 7, 8, 9];
const result = calculateSum(numbers);
console.log("Sum of Numbers:", result);
//d. Return all the prime numbers in an array ?
function isPrime(num) {
if (num <= 1) return false;
if (num <= 3) return true;
if (num % 2 === 0 || num % 3 === 0) return false;
for (let i = 5; i * i <= num; i += 6) {
if (num % i === 0 || num % (i + 2) === 0) return false;
}
return true;
}
function getPrimeNumbers(arr) {
return arr.filter(function (num) {
return isPrime(num);
});
}
const numbers = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13];
const primeNumbers = getPrimeNumbers(numbers);
console.log("Prime Numbers:", primeNumbers);
//e. Return all the palindromes in an array
function isPalindrome(str) {
const cleanStr = str.replace(/[^a-zA-Z0-9]/g, "").toLowerCase();
return cleanStr === cleanStr.split("").reverse().join("");
}
function getPalindromes(arr) {
return arr.filter(function (str) {
return isPalindrome(str);
});
}
const words = ["racecar", "level", "hello", "deified", "world", "madam"];
const palindromes = getPalindromes(words);
console.log("Palindromes:", palindromes);
//f. Return median of two sorted arrays of the same size.
function findMedianSortedArrays(arr1, arr2) {
const mergedArray = mergeSortedArrays(arr1, arr2);
const length = mergedArray.length;
const middle = Math.floor(length / 2);
if (length % 2 === 0) {
return (mergedArray[middle - 1] + mergedArray[middle]) / 2;
} else {
return mergedArray[middle];
}
}
function mergeSortedArrays(arr1, arr2) {
const merged = [];
let i = 0;
let j = 0;
while (i < arr1.length && j < arr2.length) {
if (arr1[i] < arr2[j]) {
merged.push(arr1[i]);
i++;
} else {
merged.push(arr2[j]);
j++;
}
}
while (i < arr1.length) {
merged.push(arr1[i]);
i++;
}
while (j < arr2.length) {
merged.push(arr2[j]);
j++;
}
return merged;
}
const array1 = [1, 3, 5];
const array2 = [2, 4, 6];
const median = findMedianSortedArrays(array1, array2);
console.log("Median:", median);
//g. Remove duplicates from an array ?
function removeDuplicates(arr) {
const uniqueArray = [];
for (let i = 0; i < arr.length; i++) {
if (uniqueArray.indexOf(arr[i]) === -1) {
uniqueArray.push(arr[i]);
}
}
return uniqueArray;
}
const arrayWithDuplicates = [1, 2, 2, 3, 4, 4, 5, 6, 6];
const arrayWithoutDuplicates = removeDuplicates(arrayWithDuplicates);
console.log("Array without Duplicates:", arrayWithoutDuplicates);
//h. Rotate an array by k times
function rotateArray(arr, k) {
const n = arr.length;
k = k % n;
const firstPart = arr.slice(0, n - k);
const secondPart = arr.slice(n - k);
return secondPart.concat(firstPart);
}
const originalArray = [1, 2, 3, 4, 5];
const rotations = 3;
const rotatedArray = rotateArray(originalArray, rotations);
console.log("Rotated Array:", rotatedArray);
//2. Do the below programs in arrow functions.
//a. Print odd numbers in an array
const numbers = [1, 2, 3, 4, 5, 6, 7, 8, 9];
numbers.forEach((num) => {
if (num % 2 !== 0) {
console.log(num);
}
});
//b. Convert all the strings to title caps in a string array
const stringArray = ["hello world", "javascript is awesome", "web development"];
const titleCaseArray = stringArray.map((str) => {
return str
.split(" ")
.map((word) => {
return word.charAt(0).toUpperCase() + word.slice(1).toLowerCase();
})
.join(" ");
});
console.log("Title Case Strings:", titleCaseArray);
//c. Sum of all numbers in an array
const numbers = [1, 2, 3, 4, 5, 6, 7, 8, 9];
const sum = numbers.reduce(
(accumulator, currentValue) => accumulator + currentValue,
0
);
console.log("Sum of Numbers:", sum);
//d. Return all the prime numbers in an array
const isPrime = (num) => {
if (num <= 1) return false;
if (num <= 3) return true;
for (let i = 2; i * i <= num; i++) {
if (num % i === 0) return false;
}
return true;
};
const numbers = [1, 2, 3, 4, 5, 6, 7, 8, 9];
const primeNumbers = numbers.filter((num) => isPrime(num));
console.log("Prime Numbers:", primeNumbers);
//e. Return all the palindromes in an array
const isPalindrome = (str) => {
const cleanStr = str.replace(/[^a-zA-Z0-9]/g, "").toLowerCase();
return cleanStr === cleanStr.split("").reverse().join("");
};
const words = ["racecar", "level", "hello", "deified", "world", "madam"];
const palindromes = words.filter((str) => isPalindrome(str));
console.log("Palindromes:", palindromes);