-
Notifications
You must be signed in to change notification settings - Fork 0
/
script.js
183 lines (128 loc) · 3.93 KB
/
script.js
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
// Anonymous function.
//Print odd numbers in an array.
var arr = [12,13,14,15,17,19,21]
var result =[];
var odd = function(arr){
for(var i=0;i<arr.length;i++){
if(arr[i]%2!=0){
result.push(arr[i]);
}
}
return result;
}
console.log(odd(arr)); //output:[13,15,17,19,21]
//Convert all the strings to title caps in a string array.
var stringArray = ["hello", "world"];
var result = stringArray.map(function(string) {
return string.charAt(0).toUpperCase() + string.slice(1);
});
console.log(result); //output: ["Hello", "World"]
//Sum of all numbers in an array
var arr=[2,4,5,6,8];
var count=0;
var sum=function(arr){
for(var i=0;i<arr.length;i++){
count=count+arr[i];
}
return count;
}
console.log(sum(arr));//output:[25]
//Return all the prime numbers in an array
var arr=[2,3,4,5,6,7,8,9,10];
var getPrimeNumbers = function(arr) {
var primeNumbers = [];
for (var i = 0; i < arr.length; i++) {
var prime = true;
for (var j = 2; j < arr[i]; j++) {
if (arr[i] % j == 0) {
prime = false;
break;
}
}
if (prime) {
primeNumbers.push(arr[i]);
}
}
return primeNumbers;
};
console.log(getPrimeNumbers(arr)); //output: [2,3,5,7]
//Return all the palindromes in an array
let arr = ["malayalam", "bob", "jim", "hannah", "racecar"];
let palindromes = arr.filter(function(word) {
let reverseWord = word.split("").reverse().join("");
return word === reverseWord;
});
console.log(palindromes); //output: ["malayalam", "bob", "hannah", "racecar"]
//Return median of two sorted arrays of the same size.
let arr1=[1,2,3,6,7];
let arr2=[4,6,8,10,11];
function medianOfTwoSortedArrays(arr1, arr2) {
let mergedArray = arr1.concat(arr2).sort((a, b) => a - b);
let midIndex = Math.floor(mergedArray.length / 2);
let median;
if (mergedArray.length % 2 === 0) {
median = (mergedArray[midIndex-1] + mergedArray[midIndex]) / 2;
} else {
median = mergedArray[midIndex];
}
return median;
}
console.log(medianOfTwoSortedArrays(arr1,arr2));//output:6
//Remove duplicates from an array
let arr = [1,2,2,3,3,4,5];
let result = arr.filter(function(item,index){
return arr.indexOf(item) === index;
});
console.log(result); //output: [1, 2, 3, 4, 5]
//=> Arrow Functions.
//Print odd numbers in an array
var odd = (arr)=>{
var result =[];
for(var i=0;i<arr.length;i++){
if(arr[i]%2!=0){
result.push(arr[i]);
}
}
return result;
}
console.log(odd([12,13,14,15,17,19,21]));// output:[ 13, 15, 17, 19, 21 ]
//=>Convert all the strings to title caps in a string array.
let strArray = ["hello","world","welcome","to","guvi"];
let titleCaps = strArray.map(str => str.charAt(0).toUpperCase() + str.slice(1));
console.log(titleCaps); // Output: ["Hello", "World", "Welcome", "To", "guvi"]
//=>Sum of all numbers in an array
var sum=(arr)=>{
var count=0;
for(var i=0;i<arr.length;i++){
count=count+arr[i];
}
return count;
}
console.log(sum([2,4,5,6,8]));//output:25
//=>Return all the prime numbers in an array
var getPrimeNumbers = (arr)=> {
var primeNumbers = [];
for (var i = 0; i < arr.length; i++) {
var prime = true;
for (var j = 2; j < arr[i]; j++) {
if (arr[i] % j == 0) {
prime = false;
break;
}
}
if (prime) {
primeNumbers.push(arr[i]);
}
}
return primeNumbers;
};
console.log(getPrimeNumbers([2,3,4,5,6,7,8,9,10])); //output: [2,3,5,7]
//=>Return all the palindrome
const palindrome = (arr) => {
const result = arr.filter(str => {
const reversed = str.split("").reverse().join("");
return str === reversed;
});
return result;
}
console.log(palindrome(["malayalam"]));//output:malayalam