-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfunctions.js
44 lines (29 loc) · 931 Bytes
/
functions.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
function decimalToBinary() {
var result = [], i;
var x = prompt("Enter the decimal number");
for (i = x; i > 0; i = parseInt(i / 2)) {
result.push(i % 2);
}
result = result.reverse();
result = result.join("");
document.getElementById("answer").innerHTML = "The result is : " + result;
}
function checkArmstrong() {
// 153 = 1^3 + 5^3 + 3^3
var number = prompt("Enter a Number (positive integer)");
var y = number; //copy of number
var digits = [], i, total=0;
while (y > 0) {
digits.push(y % 10);
y = parseInt(y / 10);
}
for (i = 0; i < digits.length; i++) {
total += digits[i] * digits[i] * digits[i];
}
if (total == number) {
document.getElementById("answer").innerHTML = "It is an Amstrong Number";
}
else {
document.getElementById("answer").innerHTML = "It is not an Amstrong Number";
}
}