Travis build: 1010

This commit is contained in:
30secondsofcode
2018-01-04 09:25:51 +00:00
parent 107d72815a
commit c68e59cb1c
4 changed files with 70 additions and 10 deletions

View File

@ -340,6 +340,15 @@ average(1, 2, 3);
</details>
### _Uncategorized_
<details>
<summary>View contents</summary>
* [`luhnCheck`](#luhncheck)
</details>
---
## 🔌 Adapter
@ -5358,6 +5367,40 @@ yesNo('Foo', true); // true
<br>[⬆ 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
```
<br>[⬆ back to top](#table-of-contents)
## Collaborators

File diff suppressed because one or more lines are too long

View File

@ -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
```

View File

@ -96,6 +96,7 @@ JSONToFile:node
last:array
lcm:math
lowercaseKeys:object
luhnCheck:uncategorized
mapObject:array
mask:string
maxN:array