Files
freeCodeCamp/curriculum/challenges/english/08-coding-interview-prep/rosetta-code/sedols.english.md
Randell Dawson 04f18e43f6 fix(curriculum): Remove unnecessary assert message argument from English Coding Interview Prep challenges - 02 (#36412)
* fix: removed assert msg argument

* fix: removed msgs surrounded by 2 single quotes

* fix: removed missing 2 assert msg arguments

* fix: remove msg surrounded by two single quotes

* fix: removed unnecessary assert msg args

* fix; remove msgs surrounded by double quotes

* fix: removed unnecessary assert msg args

* fix: remove unnecessary assert msg args

* fix: removed unnecessary assert msg arg

* fix: removed unnecessary assert msg args

* fix: removed unnecessary assert msg arg

* fix: removed unnecessary assert msg args

* fix: removed unnecessary assert msg args

* fix: removed unnecessary assert msg args

* fix: removed unnecessary assert msg args

* fix: removed unnecessary assert msg args

* fix: removed unnecessary assert msg arg

* fix: removed unnecessary assert msg args

* fix: Restore expected values to assertions

* fix: remove assertion message

Co-authored-by: Vivek Agrawal <vivekmittalagrawal@gmail.com>
2019-07-26 14:24:52 +02:00

2.1 KiB

title, id, challengeType
title id challengeType
SEDOLs 59d9c6bc214c613ba73ff012 5

Description

For each number list of 6-digit SEDOLs, 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:

710889 => 7108899
B0YBKJ => B0YBKJ7
406566 => 4065663
B0YBLH => B0YBLH2
228276 => 2282765
B0YBKL => B0YBKL9
557910 => 5579107
B0YBKR => B0YBKR5
585284 => 5852842
B0YBKT => B0YBKT7
B00030 => B000300

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.

Instructions

Tests

tests:
  - text: <code>sedol</code> is a function.
    testString: assert(typeof sedol === 'function');
  - text: <code>sedol('a')</code> should return null.
    testString: assert(sedol('a') === null);
  - text: <code>sedol('710889')</code> should return '7108899'.
    testString: assert(sedol('710889') === '7108899');
  - text: <code>sedol('BOATER')</code> should return null.
    testString: assert(sedol('BOATER') === null);
  - text: <code>sedol('228276')</code> should return '2282765'.
    testString: assert(sedol('228276') === '2282765');

Challenge Seed

function sedol(input) {
  // Good luck!
  return true;
}

Solution

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();
}