A Windows 95 cd key has ten digits, in the form of XXX-XXXXXXX.
The first segment (also named a site) must be 3 digits long, but cannot be 333, 444, 555, 666, 777, 888, or 999.
The second segment must be 7 digits long, divisble by 7, but the last digit can not be 0 or ≥ 8.
(adapted from this article)
function validateKey(key) {
var segments = key.split('-');
var illegalSites = [333, 444, 555, 666, 777, 888, 999];
var illegalEnds = [0, 8, 9];
if (segments.length != 2
|| illegalSites.includes(+(segments[0])) || +(segments[0]) > 999 || +(segments[0]) < 0 || segments[0].length != 3
|| +(segments[1]) % 7 == true || illegalEnds.includes(+(segments[1].slice(-1))) || segments[1].length != 7) {return false}
return true;
}
// TODO: Add your tests here
// Starting from Node 10.x, [Mocha](https://mochajs.org) is used instead of our custom test framework.
// [Codewars' assertion methods](https://github.com/Codewars/codewars.com/wiki/Codewars-JavaScript-Test-Framework)
// are still available for now.
//
// For new tests, using [Chai](https://chaijs.com/) is recommended.
// You can use it by requiring:
// const assert = require("chai").assert;
// If the failure output for deep equality is truncated, `chai.config.truncateThreshold` can be adjusted.
describe("Solution", function() {
it("validate keys", function() {
Test.assertEquals(validateKey('879-5676524'), true);
Test.assertEquals(validateKey('879-5676529'), false);
Test.assertEquals(validateKey('535-5676524'), true);
Test.assertEquals(validateKey('000-5676524'), true);
Test.assertEquals(validateKey('999-5676524'), false);
Test.assertEquals(validateKey('764-2365839'), false);
Test.assertEquals(validateKey('3-5676524'), false);
Test.assertEquals(validateKey('364-17688'), false);
Test.assertEquals(validateKey('5676524'), false);
Test.assertEquals(validateKey('3-'), false);
});
});