-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
62 lines (55 loc) · 1.78 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
const expect = require('chai').expect;
const MajorColors = [
"white",
"red",
"black",
"yellow",
"violet"
];
const MinorColors = [
"blue",
"orange",
"green",
"brown",
"slate"
];
function GetColorFromPairNumber(pairNumber) {
let colorPair = {};
const zeroBasedPairNumber = pairNumber - 1;
const majorColorIndex =
Math.floor(zeroBasedPairNumber / MinorColors.length);
colorPair.major = MajorColors[majorColorIndex];
colorPair.minor = MinorColors[zeroBasedPairNumber % MinorColors.length];
return colorPair
}
function GetPairNumberFromColors(majorColor, minorColor) {
let majorIndex = 0;
let minorIndex = 0;
for(majorIndex = 0; majorIndex < MajorColors.length; majorIndex++) {
if(MajorColors[majorIndex] == majorColor) {
break;
}
}
for(minorIndex = 0; minorIndex < MinorColors.length; minorIndex++) {
if(MinorColors[minorIndex] == minorColor) {
break;
}
}
return majorIndex * MinorColors.length + minorIndex + 1;
}
function testNumberToPair(number, expectedMajor, expectedMinor) {
const pairOfColors = GetColorFromPairNumber(number);
console.log(`${number} = ${expectedMajor} ${expectedMinor}`);
expect(pairOfColors.major).equals(expectedMajor);
expect(pairOfColors.minor).equals(expectedMinor);
}
function testColorToNumber(majorColor, minorColor, expectedNumber) {
const pairNumber = GetPairNumberFromColors(majorColor, minorColor);
console.log(`${majorColor} ${minorColor} = ${pairNumber}`);
expect(pairNumber).to.equal(expectedNumber);
}
console.log("color coder");
testNumberToPair(4, "white", "brown");
testNumberToPair(25, "violet", "slate");
testColorToNumber("black", "brown", 14);
testColorToNumber("yellow", "green", 18);