Created
July 27, 2023 14:51
-
-
Save berkesayin/25bef2249ee3c671cb78460bccb6849e to your computer and use it in GitHub Desktop.
This file contains 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
// Validation For Credit Card Numbers Using Luhn Algorithm | |
const validateCreditCardNumber = (input) => { | |
// Kredi kartı numarasını sayısal bir diziye dönüştürme | |
let creditCardInt = input.split("").map(Number); | |
// Luhn Algoritması gereği her ikinci basamağı sondan başlayarak ikiyle çarparız | |
for (let i = creditCardInt.length - 2; i >= 0; i = i - 2) { | |
// Seçilen her ikinci basamağı geçici bir değişkende saklarız | |
let tempValue = creditCardInt[i]; | |
// Seçilen basamağı ikiyle çarparız | |
tempValue = tempValue * 2; | |
/* Eğer ikiyle çarpma sonucu elde edilen değer 9'dan büyükse | |
sonuç iki basamaklı bir sayı olacağından dolayı basamaklar toplanarak düzeltilir */ | |
if (tempValue > 9) { | |
tempValue = (tempValue % 10) + 1; | |
} | |
// Düzeltme yapılan değerin orjinal dizideki ilgili basamağın üzerine yazılması | |
creditCardInt[i] = tempValue; | |
} | |
// Tüm basamakların toplamınının hesaplanması | |
let total = 0; | |
for (let i = 0; i < creditCardInt.length; i++) { | |
total += creditCardInt[i]; | |
} | |
/* Toplamın 10'a bölümünden kalanı kontrol edilmesi ve kredi kartı numarasının geçerli | |
olup olmadığının belirlenmesi */ | |
return total % 10 === 0; | |
}; | |
module.exports = validateCreditCardNumber; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment