-
Notifications
You must be signed in to change notification settings - Fork 0
/
Task 5.2.html
105 lines (81 loc) · 1.96 KB
/
Task 5.2.html
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
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Task 5.2</title>
</head>
<body>
<h2> Arrow function </h2>
<script>
// // A. print odd numbers in an array
let odd =(numbers)=>
{
for (let i=1; i<10; i++)
{
if(i%2 != 0)
{
console.log(i)
}
}
}
let numbers=[1,2,3,4,5,6,7,8,9,10];
(odd(numbers));
// B. Title cap all the string in an array
let arr = ["this is a javascript","title cap","string array"];
let titlecapArray = arr.map(stringArr =>
{
let strArr = stringArr.toLowerCase();
let splitArr = strArr.split(' ');
let titleCap = splitArr.map(word => {
return (word.charAt(0).toUpperCase() + word.slice(1));
});
return titleCap.join(' ');
})
console.log(titlecapArray);
// // C. sum of all numbers in an array
let sum = (num)=>
{
let sum=0;
for (let i=0; i<=10; i++)
{
sum=sum + i;
console.log(i,sum);
}
return sum
}
let num = [1,2,3,4,5,6,7,8,9,10];
console.log(sum(num));
// // D. Return all prime numbers in an array
let arr1 = [1,2,3,4,5,6,7,8,9,10,11,12,13,14,15];
let isPrimeNumber = num =>{
for(let i=2; i<num; i++)
{
if(num % i === 0)
{
return false;
}
}
return true;
}
let primeNumber = arr1.filter(isPrimeNumber);
console.log(primeNumber);
// E. Return all the palindrome in an array
let arr2 = ["Malayalam", 12121, "level", 2222, "Hello", 2341, "Madam"];
let isPalindrome =(arr)=>
{
let palindrome = [];
for(let i=0; i<arr.length; i++){
let str = arr[i].toString().toLowerCase();
let reversed = str.split('').reverse().join('');
if(str === reversed){
palindrome.push(arr[i]);
}
}
console.log(palindrome);
}
isPalindrome(arr2);
</script>
</body>
</html>