-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
169 lines (168 loc) · 4.8 KB
/
index.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
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
module.exports = {
findMaxOfTwo: (number1, number2) => {
let max;
if (typeof number1 !== 'number' || typeof number2 !== 'number') {
throw new Error('Incompatible types: Arguments contains non numeric values');
}
else {
if (number1 > number2) {
max = number1;
}
else {
max = number2;
}
return max;
}
},
findMinOfTwo: (number1, number2) => {
let min;
if (typeof number1 !== 'number' || typeof number2 !== 'number') {
throw new Error('Incompatible types: Arguments contains non numeric values');
}
else {
if (number1 < number2) {
min = number1;
}
else {
min = number2;
}
return min;
}
},
sortArray: (array) => {
if (!Array.isArray(array)) {
throw new Error('Incompatible type: Argument is not an array');
}
else {
for (let i = 0; i < array.length; i++){
for (let j = 1; j < array.length-i; j++){
if (array[j-1] > array[j])
{
let temp = array[j-1];
array[j-1] = array[j];
array[j] = temp;
}
}
}
return JSON.stringify(array);
}
},
findMaxOfArray: (array) => {
if (!Array.isArray(array)) {
throw new Error('Incompatible type: Argument is not an array');
}
else {
let max = array[0];
for(let i =0; i < array.length; i++) {
if (array[i] > max) {
max = array[i];
}
else {
continue;
}
}
return max;
}
},
findMinOfArray: (array) => {
if (!Array.isArray(array)) {
throw new Error('Incompatible type: Argument is not an array');
}
else {
let min = array[0];
for(let i = 0; i < array.length; i++) {
if (array[i] < min) {
min = array[i];
}
else {
continue;
}
}
return min;
}
},
findFactorial: (number) => {
if (typeof number !== 'number') {
throw new Error('Incompatible type: Argument is not a number');
}
else {
let factorial = 1;
for (let i = 1; i <= number; i++) {
factorial = factorial * i;
}
return factorial;
}
},
isPrimeNumber: (number) => {
if (typeof number !== 'number') {
throw new Error('Incompatible type: Argument is not a number');
}
else {
let n = number/2;
let flag = 0;
let isPrime = false;
if (number === 0 || number === 1) {
isPrime = false
}
else {
for (let i = 2; i <= n; i++) {
if (number % i === 0) {
isPrime = false;
flag = 1;
break;
}
}
if (flag === 0) {
isPrime = true;
}
}
return isPrime;
}
},
isArmstrongNumber: (number) => {
if (typeof number !== 'number') {
throw new Error('Incompatible type: Argument is not a number');
}
else {
let original = number;
let sum = 0;
let digits = 0;
while(number > 0){
digits = digits + 1;
number = parseInt(number/10);
}
number = original;
while (number > 0){
let remainder = parseInt(number%10);
sum = sum + Math.pow(remainder, digits);
number = parseInt(number/10);
}
if (sum === original) {
return true;
}
else {
return false;
}
}
},
isPalindromeNumber: (number) => {
if (typeof number !== 'number') {
throw new Error('Incompatible type: Argument is not a number');
}
else {
let original = number;
let sum = 0;
while (number > 0){
let num = parseInt(number%10);
sum = (sum*10) + num;
number = parseInt(number/10);
}
if (sum === original) {
return true;
}
else {
return false;
}
}
}
}