TwilioQuest - The Ducktypium Classess JavaScript

Viewed 429

I was just trying to complete a challenge on https://www.twilio.com/quest.

The questions in this game are:

The Ducktypium Classess

In your code folder, create a file named ducktypium.js. Inside of it, create a class called Ducktypium. The constructor should take a single string argument, a crystal color, and store that data in an instance variable named color. The color can only be red, blue, or yellow. The constructor should throw an error if the argument is any other string.

When the class is created, it should also define a property called calibrationSequence that is initally set to be an empty array.

The Ducktypium class must implement two instance methods: refract and calibrate. Each of these functions will be described below.

The Refract Method

One of the required methods describes the refractive properties of ducktypium when it is exposed to colored light.

The refract method must take a single string argument, which must be one of red, blue, or yellow. The method should throw an error if the argument is any other string, just like the constructor. This function should return a single string, which is the color produced by the combination of the instance's color property and the color passed in to the refract function.

  • If the instance's color property is the same as the argument passed in, return that value
  • If the combination of colors is different, it should return a string which is the combination of those two primary colors.

For reference, primary colors combine in the following ways:

  • red + blue = purple
  • red + yellow = orange
  • yellow + blue = green

The Calibrate Method

The other required method creates a calibration sequence required to stabilize a ducktypium crystal.

The calibrate method takes a single argument, an array of numbers. With this input array, you must do the following:

  • Sort the numbers from smallest to largest
  • Multiply each number in the array by 3
  • Assign the resulting array to the Ducktypium instance's calibrationSequencevariable.

My Code

const VALID_COLORS = ["red", "yellow", "blue"];

class Ducktypium {
  constructor(color) {
    if (!VALID_COLORS.includes(color)) {
      throw new TypeError("Color must be red, yellow, or blue!");
    }

    this.color = color;
    this.calibrationSequence = [];
  }

  refract(pickColor) {

    if (!VALID_COLORS.includes(pickColor)) {
      throw new TypeError("Color must be red, yellow, or blue!");
    } else if (this.color == "red" && pickColor == "blue") {
      return "purple";
    } else if (this.color == "red" && pickColor == "yellow") {
      return "orange";
    } else if (this.color == "yellow" && pickColor == "blue") {
      return "green";
    } else {
      return this.color;
    }

  }

  calibrate(arrayOfNumber) {
    let currentArray = arrayOfNumber.sort();

    currentArray.forEach(item => {
      let multiply = item * 3;
      this.calibrationSequence.push(multiply);
    });
  }
}

try {
  const badColor = new Ducktypium('pink');
} catch(e) {
  console.log('Color must be red, yellow, or blue!');
}

const dt = new Ducktypium('red');

console.log(dt.color); // prints 'red'

console.log(dt.refract('blue')); // prints 'purple'
console.log(dt.refract('red')); // prints 'red'

dt.calibrate([3, 5, 1]);

console.log(dt.calibrationSequence); // prints [3, 9, 15]

The code I wrote runs perfectly, according to the game's question. But when submitted it always appears like this:

The refract method should return the color you get when its "color" property is concatenated with other primary colors. Look at the color combinations in the "Purpose" tab.

Even if I didn't use try / catch, can anyone see where I went wrong?

Thanks

2 Answers

You have not accounted for all combinations in the refract method. You've only done eg blue+red but not red+blue.

Can I suggest you write 3 boolean methods like

const mixPurple = (a,b) =>  (a == 'red' && b == 'blue') || (a == 'blue' && b == 'red');
const mixOrange = (a,b) => ...;
const mixGreen = (a,b) => ...;

And then your code does stay fairly similar

refract(pickColor) {

    if (!VALID_COLORS.includes(pickColor)) {
      throw new TypeError("Color must be red, yellow, or blue!");
    } else if (mixPurple(this.color,pickColor)) {
      return "purple";
    } else if (mixOrange(this.color,pickColor)) {
      return "orange";
    } else if (mixGreen(this.color,pickColor)) {
      return "green";
    } else {
      return this.color;
    }

  }

As it reduces all the combinations to just 3 branches and is easy to read

Just added another OR and it passed

    refract(refColor){
        if (this.color==="red" && refColor === "blue" || this.color==="blue" && refColor === "red"){
            return "purple"
        } else if (this.color==="red" && refColor === "yellow" || this.color==="yellow" && refColor === "red"){
            return "orange"
        }  else if (this.color==="yellow" && refColor === "blue" || this.color==="blue" && refColor === "yellow"){
            return "green"
        }   else if (this.color===refColor){
            return this.color}
    }
Related