From 254bb58ab592674f1d919e2f9515d2ed1b9dca02 Mon Sep 17 00:00:00 2001 From: Isabelle Viktoria Maciohsek Date: Sun, 30 Jan 2022 13:41:42 +0200 Subject: [PATCH] Update luhnCheck --- snippets/luhnCheck.md | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/snippets/luhnCheck.md b/snippets/luhnCheck.md index ef7e66e27..dd2e831fb 100644 --- a/snippets/luhnCheck.md +++ b/snippets/luhnCheck.md @@ -2,23 +2,23 @@ title: luhnCheck tags: math,algorithm,advanced firstSeen: 2018-01-03T11:02:35+02:00 -lastUpdated: 2021-10-13T19:29:39+02:00 +lastUpdated: 2022-01-30T13:37:39+02:00 --- Implements the [Luhn Algorithm](https://en.wikipedia.org/wiki/Luhn_algorithm) used to validate a variety of identification numbers, such as credit card numbers, IMEI numbers, National Provider Identifier numbers etc. -- Use `String.prototype.split('')`, `Array.prototype.reverse()` and `Array.prototype.map()` in combination with `parseInt()` to obtain an array of digits. -- Use `Array.prototype.splice(0, 1)` to obtain the last digit. +- Use `String.prototype.split()`, `Array.prototype.reverse()` and `Array.prototype.map()` in combination with `parseInt()` to obtain an array of digits. +- Use `Array.prototype.shift()` to obtain the last digit. - Use `Array.prototype.reduce()` to implement the Luhn Algorithm. - Return `true` if `sum` is divisible by `10`, `false` otherwise. ```js const luhnCheck = num => { - let arr = (num + '') + const arr = (num + '') .split('') .reverse() .map(x => parseInt(x)); - let lastDigit = arr.splice(0, 1)[0]; + const lastDigit = arr.shift(); let sum = arr.reduce( (acc, val, i) => (i % 2 !== 0 ? acc + val : acc + ((val *= 2) > 9 ? val - 9 : val)), 0