forked from twilio/starter-node
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Ducktypium.js
110 lines (84 loc) · 3.61 KB
/
Ducktypium.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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
class Ducktypium {
color;
calibrationSequence = [];
constructor(crystalColor){
if ( this.isColorOK(crystalColor)){
this.color = crystalColor;
} else {
throw new Error('color must be red blue or yellow');
}
}
refract(color) {
let refractedColor = '';
if ( this.isColorOK(color) ) {
refractedColor = this.refractColor(this.color, color);
//console.log("works now");
} else {
throw new Error('color must be red blue or yellow');
}
return refractedColor;
}
calibrate(calibrationNumbers) {
let calibrationString = ``;
let calibratedNumbers = calibrationNumbers.sort().map( item => item * 3);
calibrationString = this.formatNumbersToString(calibratedNumbers);
console.log('cal string: ' + calibrationString);
this.calibrationSequence = calibratedNumbers;
//this.calibrationSequence = calibrationString;
}
refractColor(color1, color2) {
let refractedClr = '';
if (color1 === color2) {
refractedClr = color1;
} else if ( ((color1 === 'red') && (color2 === 'blue')) ||
((color1 === 'blue') && (color2 === 'red')) ) {
refractedClr = 'purple';
} else if ( ((color1 === 'red') && (color2 === 'yellow')) ||
((color1 === 'yellow') && (color2 === 'red'))) {
refractedClr = 'orange';
} else if ( ((color1 === 'blue') && (color2 === 'yellow')) ||
((color1 === 'yellow') && (color2 === 'blue'))) {
refractedClr = 'green';
}
return refractedClr;
}
isColorOK(color) {
let colorOk = false;
if ( (color === 'red') || (color === 'blue') || (color === 'yellow')){
colorOk = true;
}
return colorOk;
}
formatNumbersToString(calibratedArray){
let calibrationString = '[';
console.log('cal string: ' + calibrationString);
calibratedArray.forEach( function(item, index, array) {
calibrationString = calibrationString.concat(item);
console.log('Item: '+item);
if (index < array.length-1){
calibrationString = calibrationString.concat(', ');
}
console.log('cal string: ' + calibrationString);
});
calibrationString = calibrationString.concat(']');
return calibrationString;
}
}
//let ducktypium = new Ducktypium("fred");
let ducktypium = new Ducktypium("red");
console.log('ducktypium color is: ' + ducktypium.color);
console.log('refracted color is: ' + ducktypium.refract('blue'));
console.log('refracted color is: ' + ducktypium.refract('yellow'));
console.log('refracted color is: ' + ducktypium.refract('red'));
ducktypium = new Ducktypium("blue");
console.log('ducktypium color is: ' + ducktypium.color);
console.log('refracted color is: ' + ducktypium.refract('blue'));
console.log('refracted color is: ' + ducktypium.refract('yellow'));
console.log('refracted color is: ' + ducktypium.refract('red'));
ducktypium = new Ducktypium("yellow");
console.log('ducktypium color is: ' + ducktypium.color);
console.log('refracted color is: ' + ducktypium.refract('blue'));
console.log('refracted color is: ' + ducktypium.refract('yellow'));
console.log('refracted color is: ' + ducktypium.refract('red'));
ducktypium.calibrate([3, 5, 1]);
console.log('Calibration Sequence is: ' + ducktypium.calibrationSequence);