diff --git a/luhn-algorithm/script.js b/luhn-algorithm/script.js new file mode 100644 index 0000000..5a9406a --- /dev/null +++ b/luhn-algorithm/script.js @@ -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"); +} diff --git a/luhn-algorithm/style.css b/luhn-algorithm/style.css new file mode 100644 index 0000000..44364c7 --- /dev/null +++ b/luhn-algorithm/style.css @@ -0,0 +1,7 @@ +:root { + --mdc-theme-primary: #00897b; + --mdc-theme-secondary: #d84315; +} +.mdc-text-field-helper-text { + margin-bottom: 10px; +}