-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathanonymousfunction.js
More file actions
89 lines (80 loc) · 3.28 KB
/
anonymousfunction.js
File metadata and controls
89 lines (80 loc) · 3.28 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
//1 Odd numbers using anonymous function
let input = [10,12,25,46,73]
let output1= []
let odd = function(){ //anonymous function syntax
for (let no of input){
if(no%2!==0){ //Logic for odd number
output1.push(no) //Pushed odd numbers in empty array
}
} console.log(output1) //Displaying array with odd numbers
}
odd(); //Function call
//2. Convert all the strings to title caps in a string aray
let input2 = ['thomson','varsha','stephen','rio']
let output2 = [];
let cap = function(){ //anonymous function syntax
for ( i=0 ; i<input2.length; i++){ //for loop to get values from array
let str = input2[i].split(" "); // will split each value and make them seperate
// console.log(str)
for(j=0; j<str.length;j++){ //for loop to get each character seperatly
let upper = str[j].charAt(0).toUpperCase() + str[j].slice(1) //0th charatcer will get capitalised and will concat with remaining characters
output2.push(upper) //pushing to empty array
}
}console.log(output2) //displaying output
}
cap(); //Function call
//3. Sum of all numbers in an array
var addition = 0 //inital addition is 0
let sum = function() {
for(i=0; i<input.length; i++){ //for loop conndition
addition = input[i] + addition //0=10+0 and so on
} console.log(addition)
}
sum();
//4. Retum all the prime numbers in an array
let output3 =[]
let primeNo = function(){ //funvtion created for priem number
for(var no of input){
let flag = 0; //set defaualt flag = 0
for( let i=2; i<=no/2; i++){ //for loop for getting numbers from array
if(no%i==0){
flag=1 //if value is prime number change value to 1 fromm 0
break;
}
}
if (flag == 0){
output3.push(no) //if flag is 0 for given condition push value to array
}
} console.log(output3)
}
primeNo();
//5. Return all the palindromes in an array
let input3 = ['sydney', 181, 'madam','deed', 'abc', 'dad', 999]
let output4= []
let palCheck = function(){
for (let value of input3){
let str = value.toString(); //for of loop to get value from array
let flag = 1
for(i=0; i<=str.length/2; i++){ //Palindrome logic
if(str[i]!==str[str.length - 1 -i]){
flag = 0;
}
}
if(flag==1){
output4.push(value) //if its plaindrome , push to output array
}
}console.log(output4)
}
palCheck();
// 6. Remove duplicates from an array
let input6 = [10,12,25,12,73,25]
let output6 = []
let chkDuplicate = function(){
for ( let value of input6){
if(output6.indexOf(value)==-1){ //check index of value, if value is avalaible only once it will return -1
output6.push(value); //pushing aboove value to output array if consition is satisfied
}
}
console.log(output6) //Outout
}
chkDuplicate();