-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
257 lines (138 loc) · 4.04 KB
/
script.js
File metadata and controls
257 lines (138 loc) · 4.04 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
//console.log("hello");
//function add(a,b)
//{
//return a + b;
//}
//var result = add(3,8);
//console.log(result);
//1.Print odd numbers in an array
// function oddnum(numbers) {
// var splitedNumbers = numbers.split(",");
// console.log(splitedNumbers);
// var count = splitedNumbers.length;
// console.log(count);
// var kResult = []
// for (let i = 0; i < count; i++) {
// if (numbers[i] % 2 != 0) {
// kResult.push(numbers[i]);
// }
// }
// return kResult;
// }
// var numbers = window.prompt('Enter numbers with comm:');
// console.log(numbers);
// console.log(oddnum(numbers));
//1.Print The Odd Numbers in An Array:
//1st Method
//Anonyomus Function
const array = [11, 33, 57, 34, 10];
const result = [];
function odd(array) {
for (let i = 0; i < array.length; i++) {
if (array[i] % 2 != 0) {
result.push(array[i]);
}
}
return result;
}
console.log(odd(array));
//IIFT (Immediately Invoked Function Expression)
//2nd Method
(function odd(array) {
var result = [];
for (var i = 0; i < array.length; i++) {
if (array[i] % 2 != 0) {
result.push(array[i]);
}
}
console.log(result);
})([12, 13, 14, 15, 16]);
//Arrow Function (ES6)
//3rd Method
const oddnum = (array) => {
var result = [];
for (var i = 0; i < array.length; i++) {
if (array[i] % 2 != 0) {
result.push(array[i]);
}
}
return result;
};
console.log(oddnum([12, 23, 34, 45, 56, 67, 98, 21]));
//2.Convert all the strings to title caps in a string array
//1st Method
//Anonyomus Function
let mymsg = 'convert all string to title caps in a string array';
function titlecase(msg) {
// let step1 = msg.split(' ');
// let step2 = step1.map(Word => word.charAt(0).toUpperCase() + word.substr(1).toLowerCase());
// let step3 = step2.join(' ');
// console.log(step3);
var res = [];
res.push(msg.split(' ').map(word => word.charAt(0).toUpperCase() + word.substr(1).toLowerCase()).join(' '));
return res;
}
console.log(titlecase(mymsg));
//IIFT (Immediately Invoked Function Expression)
//2nd Method
(function titlecase(msg) {
var result = [];
{
result.push(msg.split(' ').map(word => word.charAt(0).toUpperCase() + word.substr(1).toLowerCase()).join(' '));
}
console.log(result);
})('convert all string to title caps in a string array');
//Arrow Function (ES6)
//3rd Method
let case1 = (titlecase) => {
var result1 = [];
result1.push(titlecase.split(' ').map(word => word.charAt(0).toUpperCase() + word.substr(1).toLowerCase()).join(' '));
return result1;
};
console.log(case1('convert all string to title caps in a string array'));
// addnum
// function AddNumber() {
// const input = window.prompt('Enter numbers in comma seprated.')
// const result = input.split(',')
// .map(i => parseInt(i))
// .reduce((a, b) => a + b, 0)
// return {
// result,
// type: typeof input
// }
// }
//3.Sum of all numbers in an array
//1st Method
//Anonyomus Function
let sum2 = [12, 23, 45, 67, , 98, 90, 56];
function addsum(accumulator, currentVal) {
// console.log("previosVal", previousVal);
// console.log("currentVal", currentVal);
return accumulator + currentVal;
}
let totalsum2 = sum2.reduce(addsum, 0)
console.log("totalsum 1 is:", totalsum2);
//IIFT (Immediately Invoked Function Expression)
//2nd Method
let sum = [12,23,45,67,,98,90,56];
let totalsum = sum.reduce(function (accumulator, currentVal) {
// console.log("previosVal", previousVal);
// console.log("currentVal", currentVal);
return accumulator + currentVal
},0);
console.log("totalsum 2 is:",totalsum);
//Arrow Function (ES6)
//3rd Method
let sum3 = [12, 23, 45, 67, , 98, 90, 56];
const addsum1 = (accumulator, currentVal) => accumulator + currentVal;
let totalsum3 = sum3.reduce(addsum, 0)
console.log("totalsum 3 is:", totalsum3);
//1st Method
//Anonyomus Function
let word = "madam";
var msg = function (word){
let letter = word.split("").reverse().join("");
let ispalindrome = word === letter;
return ispalindrome;
}
console.log(msg(word));