-
Notifications
You must be signed in to change notification settings - Fork 0
/
script.js
76 lines (63 loc) · 1.94 KB
/
script.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
let inputBtn = document.querySelector('#input_btn');
inputBtn.addEventListener('click', function () {
const numberOfRequests = parseInt(document.querySelector('input[name="nums"]:checked').value);
const numsForExpression = getValuesFromUser(numberOfRequests);
const dublicOfNums = numsForExpression.slice(0);
const commonDivisor = calcGCD(numsForExpression);
displayOutput(commonDivisor, dublicOfNums);
});
function getValuesFromUser(num) {
const array = [];
for (let i = 0; i < num; i++) {
let userAnswer = parseInt(prompt(`Enter the ${i + 1}${getDeclension(i + 1)} number`, ''));
array.push(userAnswer);
}
return array;
}
function calcGCD(array) {
let remainder;
let numA = array.shift();
let numB = array.shift();
while (remainder !== 0) {
if (numA >= numB) {
if (numA % numB === 0) {
if (array.length === 0) {
return numB;
} else if (array.length !== 0) {
numA = array.shift();
}
} else if (numA % numB !== 0) {
remainder = numA % numB;
numA = numB;
numB = remainder;
}
} else if (numA < numB) {
if (numB % numA === 0) {
if (array.length === 0) {
return numA;
} else if (array.length !== 0) {
numB = array.shift();
}
} else if (numB % numA !== 0) {
remainder = numB % numA;
numB = numA;
numA = remainder;
}
}
}
}
function displayOutput(divisor, array) {
return alert(`G.C.D.(${array}) = ${divisor}`);
}
function getDeclension(num) {
switch (num) {
case 1:
return 'st';
case 2:
return 'nd';
case 3:
return 'rd';
case 4:
return 'th';
}
}