Add files via upload
This commit is contained in:
31
luhn-algorithm/script.js
Normal file
31
luhn-algorithm/script.js
Normal file
@@ -0,0 +1,31 @@
|
||||
window.activated_drawer_item_link = "/luhn-algorithm/";
|
||||
|
||||
document.addEventListener("keyup", function(event) {
|
||||
// stop any code that may normally run when enter key pressed
|
||||
event.preventDefault();
|
||||
// if enter key is pressed, run Luhn algorithm
|
||||
if (event.keyCode === 13) {
|
||||
checkNumber();
|
||||
} else {
|
||||
// if number in input field is being edited, remove previous number evaluation message
|
||||
$("#validation_message").html(null);
|
||||
}
|
||||
});
|
||||
|
||||
// function for validating number entered with Luhn algorithm
|
||||
function checkNumber() {
|
||||
// retrieve digits & reverse
|
||||
var digits = document.getElementsByTagName("input")[0].value.split("").reverse(), sum = 0;
|
||||
digits.forEach(function(currentDigit, index) {
|
||||
newDigit = Number(currentDigit);
|
||||
if (index % 2 == 1) {
|
||||
if ((newDigit *= 2) > 9) {
|
||||
newDigit -= 9;
|
||||
}
|
||||
}
|
||||
sum += newDigit;
|
||||
});
|
||||
|
||||
// displays evaluation message
|
||||
$("#validation_message").html(((sum % 10) == 0) ? "Number is valid" : "Number is invalid");
|
||||
}
|
||||
7
luhn-algorithm/style.css
Normal file
7
luhn-algorithm/style.css
Normal file
@@ -0,0 +1,7 @@
|
||||
:root {
|
||||
--mdc-theme-primary: #00897b;
|
||||
--mdc-theme-secondary: #d84315;
|
||||
}
|
||||
.mdc-text-field-helper-text {
|
||||
margin-bottom: 10px;
|
||||
}
|
||||
Reference in New Issue
Block a user