From 7d50f43c13131e2942002b55e9c016357b1a94bf Mon Sep 17 00:00:00 2001 From: Rohit Tanwar <31792358+kriadmin@users.noreply.github.com> Date: Wed, 3 Jan 2018 14:32:35 +0530 Subject: [PATCH 1/5] Create luhnCheck.md --- snippets/luhnCheck.md | 25 +++++++++++++++++++++++++ 1 file changed, 25 insertions(+) create mode 100644 snippets/luhnCheck.md diff --git a/snippets/luhnCheck.md b/snippets/luhnCheck.md new file mode 100644 index 000000000..a6f2cbfd1 --- /dev/null +++ b/snippets/luhnCheck.md @@ -0,0 +1,25 @@ +### luhnCheck + +Implementation of 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. +Works with numbers and strings alike +``` js +const luhnCheck = num => { + let arr = (num+'').split('').reverse() + let lastDigit = arr.splice(0,1) + let sum = arr.reduce((acc,val,i) => { + val = parseInt(val) + if(i%2!==0) return acc + val + else{ + val = (val * 2)%9 || 9 + return acc+ val } + },0) + sum += parseInt(lastDigit) + return sum%10 === 0 + } +``` +```js + luhnCheck("4485275742308327"); //true + luhnCheck(4485275742308327); //true + luhnCheck(6011329933655299); // true + luhnCheck(123456789); //false +``` From 10652a52f397fbef60494e87973d5b74ad814a92 Mon Sep 17 00:00:00 2001 From: Rohit Tanwar <31792358+kriadmin@users.noreply.github.com> Date: Wed, 3 Jan 2018 17:44:36 +0530 Subject: [PATCH 2/5] Update luhnCheck.md --- snippets/luhnCheck.md | 8 +------- 1 file changed, 1 insertion(+), 7 deletions(-) diff --git a/snippets/luhnCheck.md b/snippets/luhnCheck.md index a6f2cbfd1..4ab440d43 100644 --- a/snippets/luhnCheck.md +++ b/snippets/luhnCheck.md @@ -6,13 +6,7 @@ Works with numbers and strings alike const luhnCheck = num => { let arr = (num+'').split('').reverse() let lastDigit = arr.splice(0,1) - let sum = arr.reduce((acc,val,i) => { - val = parseInt(val) - if(i%2!==0) return acc + val - else{ - val = (val * 2)%9 || 9 - return acc+ val } - },0) + let sum = arr.reduce((acc,val,i) => (acc,val,i) => i%2!==0 ? acc + parseInt(val) : acc + (parseInt(val) * 2)%9 || 9,0) sum += parseInt(lastDigit) return sum%10 === 0 } From 84ab6f1d9f87baa766d9b2578e9f4518990cf505 Mon Sep 17 00:00:00 2001 From: Rohit Tanwar <31792358+kriadmin@users.noreply.github.com> Date: Wed, 3 Jan 2018 17:56:42 +0530 Subject: [PATCH 3/5] Update luhnCheck.md --- snippets/luhnCheck.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/snippets/luhnCheck.md b/snippets/luhnCheck.md index 4ab440d43..4695a060b 100644 --- a/snippets/luhnCheck.md +++ b/snippets/luhnCheck.md @@ -6,7 +6,7 @@ Works with numbers and strings alike const luhnCheck = num => { let arr = (num+'').split('').reverse() let lastDigit = arr.splice(0,1) - let sum = arr.reduce((acc,val,i) => (acc,val,i) => i%2!==0 ? acc + parseInt(val) : acc + (parseInt(val) * 2)%9 || 9,0) + let sum = arr.reduce((acc,val,i) => i%2!==0 ? acc + parseInt(val) : acc + (parseInt(val) * 2)%9 || 9,0) sum += parseInt(lastDigit) return sum%10 === 0 } From 6e296ca45dfb51979992d1da97f92a1b94d4c2fb Mon Sep 17 00:00:00 2001 From: Angelos Chalaris Date: Thu, 4 Jan 2018 11:22:32 +0200 Subject: [PATCH 4/5] Update luhnCheck.md --- snippets/luhnCheck.md | 29 ++++++++++++++++++----------- 1 file changed, 18 insertions(+), 11 deletions(-) diff --git a/snippets/luhnCheck.md b/snippets/luhnCheck.md index 4695a060b..1903fdf90 100644 --- a/snippets/luhnCheck.md +++ b/snippets/luhnCheck.md @@ -1,19 +1,26 @@ ### luhnCheck Implementation of 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. -Works with numbers and strings alike + +Use `String.split('')`, `Array.reverse()` and `Array.map()` in combination with `parseInt()` to obtain an array of digits. +Use `Array.splice(0,1)` to obtain the last digit. +Use `Array.reduce()` to implement the Luhn Algorithm. +Return `true` if `sum` is divisible by `10`, `false` otherwise. + + ``` js const luhnCheck = num => { - let arr = (num+'').split('').reverse() - let lastDigit = arr.splice(0,1) - let sum = arr.reduce((acc,val,i) => i%2!==0 ? acc + parseInt(val) : acc + (parseInt(val) * 2)%9 || 9,0) - sum += parseInt(lastDigit) - return sum%10 === 0 - } + let arr = (num+'').split('').reverse().map(x => parseInt(x)); + let lastDigit = arr.splice(0,1)[0]; + let sum = arr.reduce((acc,val,i) => i%2!==0 ? acc + val : acc + (val * 2) % 9 || 9,0); + sum += lastDigit; + return sum%10 === 0; +} ``` + ```js - luhnCheck("4485275742308327"); //true - luhnCheck(4485275742308327); //true - luhnCheck(6011329933655299); // true - luhnCheck(123456789); //false +luhnCheck("4485275742308327"); // true +luhnCheck(4485275742308327); // true +luhnCheck(6011329933655299); // true +luhnCheck(123456789); // false ``` From 0e79b52ff63e1d4bce466f9032b0e1dd5c2deb61 Mon Sep 17 00:00:00 2001 From: Angelos Chalaris Date: Thu, 4 Jan 2018 11:23:07 +0200 Subject: [PATCH 5/5] Update luhnCheck.md --- snippets/luhnCheck.md | 1 - 1 file changed, 1 deletion(-) diff --git a/snippets/luhnCheck.md b/snippets/luhnCheck.md index 1903fdf90..cbd3918f7 100644 --- a/snippets/luhnCheck.md +++ b/snippets/luhnCheck.md @@ -20,7 +20,6 @@ const luhnCheck = num => { ```js luhnCheck("4485275742308327"); // true -luhnCheck(4485275742308327); // true luhnCheck(6011329933655299); // true luhnCheck(123456789); // false ```