Repository navigation
Expand file tree
/
Copy pathfunctionTypes.js
More file actions
224 lines (221 loc) · 5.84 KB
/
Copy pathfunctionTypes.js
File metadata and controls
224 lines (221 loc) · 5.84 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
//1.Sum of Array using Function
let a = [1,2,3,4,5,6,7,8,9];
let add = 0;
function sum(a)
{
for(var i=0;i<a.length;i++)
{
add+=a[i];
}
return add;
}
console.log("Sum of Array Using Function:");
console.log( sum(a) );
//Sum of Array Using Anonymous function:
add =0;
let sum1 = function (a)
{
for (var i=0;i<a.length;i++)
{
add+=a[i];
}
return add;
}
console.log("Sum of Array Using Anonymous Function:");
console.log(sum1(a));
//Sum of Array Using IIFE Function Method:
(function addition()
{
add=0;
for (var i=0;i<a.length;i++)
{
add+=a[i];
}
console.log("Sum of Array Using IIFE Function:");
console.log(add);
})([1,2,3,4,5,6,7,8,9]);
// Sum of Array Using Arrow Function:
let addition=(a)=>{
add=0;
for (var i=0;i<a.length;i++)
{
add+=a[i];
}
return add;
}
console.log("Sum of Array Using Arrow Function:");
console.log(addition(a));
// 2.Convert all Strings to title Caps in a String using Function:
const text = " welcome to the website";
function b(text)
{
let words = text.split(" ");
for(var i=0; i<words.length; i++)
{
words[i] = words[i].charAt(0).toUpperCase() + words[i].slice(1);
}
return words.join(" ");
}
console.log("Converting all Strings to Title Caps using Function:");
console.log ( b(text) );
//Convert all Strings to title Caps in a String using Anonymous Function:
const txt = " greetings of the day";
let words1 = txt.split(" ");
let ab = function(txt){
for(var i=0; i<words1.length; i++)
{
words1[i] = words1[i].charAt(0).toUpperCase() + words1[i].slice(1);
}
return words1.join(" ");
}
console.log("Converting all Strings to Title Caps using Anonymous Function:");
console.log (ab(txt));
//Convert all Strings to title Caps in a String using IIFE Function:
const text1= " problems given to solve, is solved using types of function";
( function convertion()
{
let words2 = text1.split(" ");
for(var i=0; i<words2.length; i++)
{
words2[i] = words2[i].charAt(0).toUpperCase() + words2[i].slice(1);
}
console.log("Converting all Strings to Title Caps using IIFE Function:");
console.log(words2.join(" "))
})(text1)
//Convert all Strings to title Caps in a String using Arrow Function:
const t = " thank you"
let convert = (t) => {
let word = t.split(" ");
for(var i=0; i<word.length; i++)
{
word[i] = word[i].charAt(0).toUpperCase() + word[i].slice(1);
}
return word.join(" ");
}
console.log("Converting all Strings to Title Caps using Arrow Function:");
console.log(convert(t));
//3. Return all Palindromes in an array using Function:
const arr = ['car','mom','dad','hello','racecar','why','did'];
let PalindromeString = [];
function palindrome(arr)
{
for (var i=0; i< arr.length; i++)
{
let p = arr[i];
let s = arr[i].split("").reverse().join("");
if(s==p)
{
PalindromeString.push(arr[i])
}
}
return PalindromeString;
}
console.log(palindrome(arr));
//Return all Palindrome in a Array using Anonymous Function:
let PalindromeString1 = [];
let output = function (arr)
{
for (var i=0; i< arr.length; i++)
{
let q = arr[i];
let r = arr[i].split("").reverse().join("");
if(q==r)
{
PalindromeString1.push(arr[i]);
}
}
return PalindromeString;
}
console.log("Return all Palindrome in a Array using Anonymous Function:")
console.log(output(arr));
//Return all Palindrome in a Array using IIFE Function:
let PalindromeString2 = [];
(function palstring( )
{
for (var i=0; i< arr.length; i++)
{
let u= arr[i];
let v = arr[i].split("").reverse().join("");
if(u==v)
{
PalindromeString2.push(arr[i]);
}
}
console.log("Return all Palindrome in a Array using IIFE Function:");
console.log(PalindromeString2);
})(arr);
//Return all Palindrome in a Array using Arrow Function:
let PalindromeString3 = [];
let ps = (arr) => {
for (var i=0; i< arr.length; i++)
{
let g= arr[i];
let h = arr[i].split("").reverse().join("");
if(g==h)
{
PalindromeString3.push(arr[i]);
}
}
return PalindromeString3;
}
console.log("Return all Palindrome in a Array using Arrow Function:");
console.log(ps(arr));
//4.Print Odd Numbers in array using function:
let A = [11,12,13,14,15,16,17,18,19,20];
let result = [];
function odd(A)
{
for(var i=0;i<A.length;i++)
{
if(A[i]%2!==0)
{
result.push(A[i])
}
}
return result;
}
console.log("Printing Odd Numbers in array using function:")
console.log(odd(A));
//Print Odd Numbers in array using Anonymous function:
let result1 = [];
let odd1 = function odd(A)
{
for(var i=0;i<A.length;i++)
{
if(A[i]%2!==0)
{
result1.push(A[i])
}
}
return result1;
}
console.log("Printing Odd Numbers in array using Anonymous function:")
console.log(odd1(A));
//Print Odd Numbers in array using IIFE function:
(function odd2(A)
{
let result2 =[];
for(var i=0;i<A.length;i++)
{
if(A[i]%2!==0)
{
result2.push(A[i])
}
}
console.log("Printing Odd Numbers in array using IIFE function:")
console.log(result2);
})(A);
//Print Odd Numbers in array using Arrow function
const odd3 =(A) => {
let result3 =[];
for(var i=0;i<A.length;i++)
{
if(A[i]%2!==0)
{
result3.push(A[i])
}
}
return result3;
}
console.log("Printing Odd Numbers in array using Arrow function:")
console.log(odd3(A));