A Pen by Godfred Asamoah on CodePen.
Created
August 14, 2018 17:31
-
-
Save gfredtech/27084be75ca75791b59d69fa654c1ddc to your computer and use it in GitHub Desktop.
Andela challenge
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<body onload=validateNumber()> | |
<div id="main" class="box"> | |
<h1>Armstrong Checker</h1> | |
<p>Enter a 3-digit number below: <p/> | |
<input type="text" id ="digits" onkeyup="validateNumber()" maxlength="3" size = 10> | |
<input type="button" value="Check" class = "button" readonly="readonly" id="my_button" onclick="buttonClick()"> | |
<div id="output" class="output"> | |
Output | |
</div> | |
</div> | |
</body> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function validateNumber() { | |
var number = document.getElementById("digits").value; | |
var regex = new RegExp("[0-9]{3}"); | |
disableButton(!regex.test(number)); | |
} | |
function disableButton(disable) { | |
var button = document.getElementById("my_button"); | |
if(disable) { | |
button.style.backgroundColor = 'rgb(221, 221, 221)'; | |
button.style.color = 'rgb(153, 153, 153)'; | |
} else { | |
button.style.color = ""; | |
button.style.backgroundColor = ""; | |
} | |
} | |
function getDigits(number) { | |
return number.toString().split('').map((x) => parseInt(x));; | |
} | |
function isArmstrong(number) { | |
let digits = getDigits(number); | |
let armstrong = Math.pow(digits[0], 3) + Math.pow(digits[1], 3) + Math.pow(digits[2], 3); | |
return number == armstrong; | |
} | |
function buttonClick() { | |
var number = document.getElementById("digits").value; | |
var button = document.getElementById("my_button"); | |
if(button.style.backgroundColor === 'rgb(221, 221, 221)' || number === "") { | |
alert("Your input must be a 3-digit number"); | |
} | |
else { | |
var output = document.getElementById("output"); | |
if (isArmstrong(number)) { | |
output.innerHTML = "Ja! It is an Armstrong number!"; | |
} else output.innerHTML = "Nein, it isn't an Armstrong number :("; | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
body { | |
background-color: aquamarine; | |
background-position: 50% 50%; | |
background-size: cover; | |
} | |
h1 { | |
color: green; | |
font-size: auto; | |
margin-left: 100px; | |
margin-right: auto; | |
width: 360px; | |
} | |
#my_button { | |
border-radius: 5px; | |
margin: 5px 5px 5px 5px; | |
height: 35px; | |
width: 55px; | |
} | |
div.box { | |
border: solid 1px #000000; | |
background-color: #f9f9f9; | |
width: 450px; | |
margin-left: auto; | |
margin-right: auto; | |
} | |
p { | |
margin: 10px 100px 25px; | |
font-weight: bold; | |
} | |
div.output { | |
color: black; | |
font-size: 20px; | |
text-align: center; | |
margin-left: auto; | |
margin-right: auto; | |
padding: 20px; | |
word-wrap: break-word; | |
background-color: gainsboro; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment