-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
150 lines (133 loc) · 3.91 KB
/
Copy pathscript.js
File metadata and controls
150 lines (133 loc) · 3.91 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
//Function
function mul(a, b, c, d) {
return a * b * c * d;
}
function add(a, b, c) {
return a + b + c;
}
function sub(a, b) {
return a - b;
}
console.log("hi");
console.log(sub(2, 1)); //1
console.log(add(1, 2, 2)); // 5
console.log(mul(1, 2, 3, 4)); //24
//Example :-Print the odd numbers in an array
var arr = [12, 13, 14, 15, 16];
var result = [];
//function is used to extract the values(odd array)
function odd(arr) {
for (let i = 0; i < arr.length; i++) {
if (arr[i] % 2 != 0) {
result.push(arr[i]);
}
}
return result;
}
console.log(odd(arr));
//function without parameter
function greet() {
return "welcome";
}
greet();
//Anonyomus Function
//Example :-Print the odd numbers in an array
var arr = [12, 13, 14, 15, 16];
var result = [];
var odd = function (arr) {
for (let i = 0; i < arr.length; i++) {
if (arr[i] % 2 != 0) {
result.push(arr[i]);
}
}
return result;
};
console.log(odd(arr));
//IIFE (Immediately Invoked Function Expression)
//Example :-Print the odd numbers in an array
(function odd(arr) {
var result = [];
for (var i = 0; i < arr.length; i++) {
if (arr[i] % 2 != 0) {
result.push(arr[i]);
}
}
console.log(result);
})([12, 13, 14, 15, 16]);
//Arrow Function (ES6)
//Example :-Print the odd numbers in an array
const od = (arr) => {
var result = [];
for (var i = 0; i < arr.length; i++) {
if (arr[i] % 2 != 0) {
result.push(arr[i]);
}
}
return result;
};
console.log(od([1, 2, 3, 4, 5, 6, 7, 8, 9]));
//Sum of all numbers in an array using function
function add(x, y) {
return x + y;
}console.log(add(2,3));
//Sum of all numbers in an array using arrow function
var arr = [1, 2, 3, 4, 5];
var sum = arr.reduce((a, b) => a + b);
console.log(sum);
//Sum of all numbers in an array using anonymous function and IIFE
var arr = [1, 2, 3, 4, 5];
var sum = (function(arr) {
return arr.reduce(function(a, b) {
return a + b;
});
})(arr);
console.log(sum);
//Convert all the strings to title caps in a string array using function
function titleCase(str) {
return str.toLowerCase().split(' ').map(function(word) {
return word.replace(word[0], word[0].toUpperCase());
}).join(' ');
}
var arr = ["hello welcome", "here is a program to", "display uppercase"];
var newArr = arr.map(titleCase);
console.log(newArr);
//Convert all the strings to title caps in a string array using anonymous function
var arr = ["hello welcome", "here is a program to", "display uppercase"];
var newArr = arr.map(function(str) {
return str.toLowerCase().split(' ').map(function(word) {
return word.replace(word[0], word[0].toUpperCase());
}).join(' ');
});
console.log(newArr);
//Convert all the strings to title caps in a string array using arrow function
var arr = ["hello world", "this is a test", "another test"];
var newArr = arr.map(str => str.toLowerCase().split(' ').map(word => word.replace(word[0], word[0].toUpperCase())).join(' '));
console.log(newArr);
//Convert all the strings to title caps in a string array using IIFE function
var arr = ["hello world", "this is a test", "another test"];
var newArr = (function(arr) {
return arr.map(function(str) {
return str.toLowerCase().split(' ').map(function(word) {
return word.replace(word[0], word[0].toUpperCase());
}).join(' ');
});
})(arr);
console.log(newArr);
//Return all the palindromes in an array using function
function isPalindrome(str) {
var len = str.length;
for (var i = 0; i < len / 2; i++) {
if (str[i] !== str[len - 1 - i]) {
return false;
}
}
return true;
}
var arr = ["madam", "country", "civic", "mom"];
var palindromes = [];
for (var i = 0; i < arr.length; i++) {
if (isPalindrome(arr[i])) {
palindromes.push(arr[i]);
}
}
console.log(palindromes);