Алгоритм Luhn JavaScript
/*
HTML:
<form action="">
<label for="cardNum">Card Number</label><br>
<input type="text" id="cardNum" placeholder="Please type your credit card number here..."><br>
</form>
<button onclick="checkIfPassLuhn()" class="checker">Check if the above number passes the Luhn Algorithm</button>
*/
function checkIfPassLuhn() {
var num = document.getElementById("cardNum").value;
var array = (num + '').split('').reverse().map(x => parseInt(x));
var theLastDig = array.shift();
let sum = array.reduce((prevVal, curVal, index) => (index % 2 !== 0 ? prevVal + curVal : prevVal + ((curVal *= 2) > 9 ? curVal - 9 : curVal)), 0);
sum += theLastDig;
if(sum % 10 === 0){
alert("Your number is correct!");
}else{
alert("Your number is incorrect");
}
}
//Call the method with an HTML element
Fair Flamingo