diff --git a/README.md b/README.md index 99e8fd6ed..212ddbe61 100644 --- a/README.md +++ b/README.md @@ -340,6 +340,15 @@ average(1, 2, 3); +### _Uncategorized_ + +
+View contents + +* [`luhnCheck`](#luhncheck) + +
+ --- ## 🔌 Adapter @@ -5358,6 +5367,40 @@ yesNo('Foo', true); // true
[⬆ Back to top](#table-of-contents) +--- + ## _Uncategorized_ + +### 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. + +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() + .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(6011329933655299); // true +luhnCheck(123456789); // false +``` + +
[⬆ back to top](#table-of-contents) + ## Collaborators diff --git a/docs/index.html b/docs/index.html index 68fbe8ebb..0098a8ded 100644 --- a/docs/index.html +++ b/docs/index.html @@ -33,7 +33,7 @@ document.body.removeChild(textArea); } }, false); - }

logo 30 seconds of code Curated collection of useful JavaScript snippets that you can understand in 30 seconds or less.

 

Adapter

call

Given a key and a set of arguments, call them when given a context. Primarily useful in composition.

Use a closure to call a stored key with stored arguments.

const call = (key, ...args) => context => context[key](...args);
+      }

logo 30 seconds of code Curated collection of useful JavaScript snippets that you can understand in 30 seconds or less.

 

Adapter

call

Given a key and a set of arguments, call them when given a context. Primarily useful in composition.

Use a closure to call a stored key with stored arguments.

const call = (key, ...args) => context => context[key](...args);
 
Promise.resolve([1, 2, 3])
   .then(call('map', x => 2 * x))
   .then(console.log); //[ 2, 4, 6 ]
@@ -1203,4 +1203,17 @@ console.log<
 yesNo('yes'); // true
 yesNo('No'); // false
 yesNo('Foo', true); // true
-
\ No newline at end of file +

Uncategorized

luhnCheck

Implementation of the Luhn Algorithm used to validate a variety of identification numbers, such as credit card numbers, IMEI numbers, National Provider Identifier numbers etc.

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.

const luhnCheck = num => {
+  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;
+};
+
luhnCheck('4485275742308327'); // true
+luhnCheck(6011329933655299); //  true
+luhnCheck(123456789); // false
+
\ No newline at end of file diff --git a/snippets/luhnCheck.md b/snippets/luhnCheck.md index cbd3918f7..9f931662a 100644 --- a/snippets/luhnCheck.md +++ b/snippets/luhnCheck.md @@ -8,18 +8,21 @@ 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().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); +```js +const luhnCheck = num => { + 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; -} + return sum % 10 === 0; +}; ``` ```js -luhnCheck("4485275742308327"); // true +luhnCheck('4485275742308327'); // true luhnCheck(6011329933655299); // true luhnCheck(123456789); // false ``` diff --git a/tag_database b/tag_database index 5ef49198b..0c13b395e 100644 --- a/tag_database +++ b/tag_database @@ -96,6 +96,7 @@ JSONToFile:node last:array lcm:math lowercaseKeys:object +luhnCheck:uncategorized mapObject:array mask:string maxN:array