-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathArray.js
More file actions
318 lines (241 loc) · 7.57 KB
/
Array.js
File metadata and controls
318 lines (241 loc) · 7.57 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
//1 Find the largest item from a given list
let array = [3, 6, 2, 56, 32, 5, 89, 32];
let largest = 0;
for (i = 0; i < array.length; i++) {
if (array[i] > largest) {
largest = array[i];
}
}
console.log(largest);
// In this method, what we do is that we compare the index of all the items of an array with the index of the first time that number occurs. If they don’t match, that implies that the element is a duplicate.All such elements are returned in a separate array using the filter() method.
// with function
// let array = [3, 6, 2, 56, 32, 5, 89, 32];
const largestNumber = (array) => {
let largest = 0;
for (let i = 0; i < array.length; i++) {
if (array[i] > largest) {
largest = array[i];
}
}
return largest;
};
console.log(largestNumber(array));
// ==================================================================
// ==================================================================
// 2 .
function reverseArr(input) {
let reverse = [];
for (let i = input.length - 1; i >= 0; i--) {
reverse = [...reverse, input[i]];
}
return reverse;
}
let list = [3, 5, 7, 8];
let result = reverseArr(list);
console.log(result);
// =================================================
// ============================================
let array = [12, 5, 8, 3, 17];
let element = 10; //Element to be searched
const checkIncludes = (array, element) => {
for (let i = 0; i < array.length; i++) {
if (array[i] === element) {
return true;
}
}
return false;
};
console.log(checkIncludes(array, element));
// =====================================
// 5. Write a program to display all prime numbers within a range
// take input from the user
const lowerNumber = 1
let higherNumber = 10000
console.log(`The prime numbers between ${lowerNumber} and ${higherNumber} are:`);
// looping from lowerNumber to higherNumber
for (let i = lowerNumber; i <= higherNumber; i++) {
let flag = 0;
// looping through 2 to user input number
for (let j = 2; j < i; j++) {
if (i % j == 0) {
flag = 1;
break;
}
}
// if number greater than 1 and not divisible by other numbers
if (i > 1 && flag == 0) {
console.log(i);
}
}
// ============================================================================
// =============================================================================
//6 Reverse a given integer number
const reversedNum = num => parseFloat(num.toString().split('').reverse().join('')) * Math.sign(num)
// or With the regular function :
function reversedNum(num) {
return (
parseFloat(num.toString().split("").reverse().join("")) * Math.sign(num)
);
}
// console.log(reversedNum(123456789));
// =========================================================
//problem-7
let arr1 = [1, 1, 2, 2, 3, 8, 4, 6];
const printOddIndex = (arr) => {
for (let i = 0; i < arr.length; i++) {
if (i % 2 == 0) {
console.log(arr[i]);
}
}
}
printOddIndex(arr1);
// =============================================================
//problem-8
let array = [3, 6, 2, 56, 32, 5, 89, 32];
const largestNumber1 = (array) => {
let largest = 0;
for (let i = 0; i < array.length; i++) {
if (array[i] > largest) {
largest = array[i];
}
}
return largest;
}
console.log(largestNumber1(array));
//9
let str = "I want to count the number of occurrences of each char in this string";
const occurrencesCount = (str) => {
let count = {};
for (let i = 0; i < str.length; i++) {
let char = str[i];
count[char] = count[char] ? count[char] + 1 : 1;
}
return count;
}
console.log(occurrencesCount(str));
//11. find the vowel from an staring :
// init the vowels
const vowels = ["a", "e", "i", "o", "u", "A", "E", "I", "O", "U"];
// create a function for find the vowels form a string.
const countVowels = (str) => {
//int the count variable
let count = 0;
//
const letters = Array.from(str);
letters.forEach((letters) => {
if (vowels.includes(letters)) {
count++;
}
});
return count;
};
console.log(countVowels("New programmer in the town"));
// ===============================================================
// 12. Write a JavaScript function that returns a passed string with letters in alphabetical order.
/*
steps:
01:Split the string into an array of characters using split() method.
02:Apply the sort() method to sort the characters in alphabetical order.
03:Finally, we use the join() method to join the characters back into a string.
*/
function alphaOrder(str) {
// 01:Split the string into an array of characters using split() method.
let arr = str.split("");
// 02:Apply the sort() method to sort the characters in alphabetical order.
arr.sort();
// 03:Finally, we use the join() method to join the characters back into a string.
let sortedStr = arr.join("");
return sortedStr;
}
console.log(alphaOrder("ikram akbar"));
// =========================================================
// 13
let text = "I want to count the number of occurrences of each char in this string";
const toUpperCase = (str) => {
const result = str.toLowerCase()
.split(' ')
.map((s) => s.charAt(0).toUpperCase() + s.substring(1))
.join(' ');
return result;
}
console.log(toUpperCase(text));
// =======================================
// 14
function unique_char(str) {
let uniql = "";
for (let x = 0; x < str.length; x++) {
let char = str.charAt(x);
if (uniql.indexOf(char) == -1 || char == ' ') {
uniql += str[x];
}
}
return uniql;
}
console.log(unique_char("the fox news newspaper"));
// ============================================
// 15
let combine = function (a, min) {
let fn = function (n, src, got, all) {
if (n == 0) {
if (got.length > 0) {
all[all.length] = got;
}
return;
}
for (let j = 0; j < src.length; j++) {
fn(n - 1, src.slice(j + 1), got.concat([src[j]]), all);
}
return;
};
let all = [];
for (let i = min; i < a.length; i++) {
fn(i, a, [], all);
}
all.push(a);
return all;
};
console.log(combine([1, 2, 3, 4], 1));
// 17. check leap year in a range
// range
function leap_year_range(st_year, end_year) {
let year_range = [];
for (let i = st_year; i <= end_year; i++) {
year_range.push(i);
}
let new_array = [];
year_range.forEach(function (year) {
if (test_LeapYear(year)) new_array.push(year);
});
return new_array;
}
function test_LeapYear(year) {
if (
(year % 4 === 0 && year % 100 !== 0) ||
(year % 100 === 0 && year % 400 === 0)
) {
return year;
} else {
return false;
}
}
console.log(leap_year_range(2000, 2012));
// find out the duplicate num from an array
// init the array
const number = [1, 2, 4, 2, 4, 6, 7];
// declare a variable for finding duplicate num form an array
// then use array.filter Method
const duplicate = number.filter((value, index, array) => {
return array.indexOf(value) == index;
});
console.log(duplicate);
// ===============================================================================
// ===============================================================================
// 18. Write an algorithm to swap two given numbers in without using a temporary variable without using any library function.
let a = 5;
let b = 15;
console.log(`the value of a = ${a} and b = ${b}`);
// the value of a = 5 and b = 15
[a, b] = [b, a];
console.log(`the value of a = ${a} and b = ${b}`);
// the value of a = 15, b = 5
// ===============================================================================