freeCodeCamp/curriculum/challenges/english/10-coding-interview-prep/rosetta-code/sedols.md

97 lines
1.8 KiB
Markdown
Raw Normal View History

---
id: 59d9c6bc214c613ba73ff012
title: SEDOLs
challengeType: 5
forumTopicId: 302305
dashedName: sedols
---
# --description--
For each number list of 6-digit [SEDOL](https://en.wikipedia.org/wiki/SEDOL "wp: SEDOL")s, calculate and append the checksum digit. That is, given the input string on the left, your function should return the corresponding string on the right:
2019-03-07 14:15:29 +00:00
<pre>
710889 => 7108899
B0YBKJ => B0YBKJ7
406566 => 4065663
B0YBLH => B0YBLH2
228276 => 2282765
B0YBKL => B0YBKL9
557910 => 5579107
B0YBKR => B0YBKR5
585284 => 5852842
B0YBKT => B0YBKT7
B00030 => B000300
</pre>
Check that each input is correctly formed, especially with respect to valid characters allowed in a SEDOL string. Your function should return `null` on an invalid input.
# --hints--
`sedol` should be a function.
```js
assert(typeof sedol === 'function');
```
`sedol('a')` should return null.
```js
assert(sedol('a') === null);
```
`sedol('710889')` should return '7108899'.
```js
assert(sedol('710889') === '7108899');
```
`sedol('BOATER')` should return null.
```js
assert(sedol('BOATER') === null);
```
`sedol('228276')` should return '2282765'.
```js
assert(sedol('228276') === '2282765');
```
# --seed--
## --seed-contents--
```js
function sedol(input) {
return true;
}
```
# --solutions--
```js
function sedol(input) {
const checkDigit = sedolCheckDigit(input);
if (checkDigit !== null) {
return input + checkDigit;
}
return null;
}
const weight = [1, 3, 1, 7, 3, 9, 1];
function sedolCheckDigit(char6) {
if (char6.search(/^[0-9BCDFGHJKLMNPQRSTVWXYZ]{6}$/) === -1) {
return null;
}
let sum = 0;
for (let i = 0; i < char6.length; i++) {
sum += weight[i] * parseInt(char6.charAt(i), 36);
}
const check = (10 - (sum % 10)) % 10;
return check.toString();
}
```