-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdraft.js
94 lines (76 loc) · 1.82 KB
/
draft.js
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
//? Problem 1
const sum = sumArray([1, 2, 3, 4, 5]);
function sumArray(array) {
let s = 0;
for (let i = 0; i < array.length; i++)
s += array[i];
return s;
}
console.log(sum); //* Output: 15
//? Problem 2
const maxNumber = findMaxNumber([10, 5, 8, 20, 3]);
function findMaxNumber(array)
{
let max=array[0];
for(let i=1;i<array.length;i++)
{
if(array[i]>max)
max=array[i];
}
return max;
}
console.log(maxNumber); //* Output: 20
//? Problem 3
const oddNumbers = filterOddNumbers([1, 2, 3, 4, 5]);
function filterOddNumbers(array)
{
let newArray=[];
for(let i=0;i<array.length;i++)
{
if(array[i]%2 != 0)
newArray.push(array[i]);
}
return newArray;
}
console.log(oddNumbers); //* [1, 3, 5]
//? Problem 4
const isPalindromeResult = isPalindrome('radar');
const isNonPalindromeResult = isPalindrome('hello');
function isPalindrome(word)
{
let reverse_word=word.split('').reverse().join('');
if(word==reverse_word)
return true;
else
return false;
}
console.log(isPalindromeResult); //*true
console.log(isNonPalindromeResult); //*false
//? Problem 5
const missingNumber = findMissingNumber([1, 2, 3, 5, 6]);
function findMissingNumber(array)
{
for(let i=0;i<array.length-1;i++)
{
if(array[i+1]==array[i]+1)
continue;
else
return array[i]+1;
}
return array[array.length-1]-1;
}
console.log(missingNumber); //* Output: 4
//? Problem 6
const duplicateNumbers = findDuplicates([1, 2, 3, 2, 4, 5, 4, 6]);
function findDuplicates(array)
{
array.sort();
let newArray=[];
for(let i=0;i<array.length-1;i++)
{
if(array[i]==array[i+1] && newArray.indexOf(array[i])==-1)
newArray.push(array[i]);
}
return newArray;
}
console.log(duplicateNumbers); //* Output: [2, 4]