You are given an array of Gem objects. Each Gem has a name
attribute, and a colors
attribute containing either a string, or an array of strings describing its color.
gems = [
{ name: 'Amethyst', colors: 'purple' },
{ name: 'Garnet', colors: ['orange', 'red']},
{ name: 'Aquamarine', colors: ['blue', 'green']}
];
Your task is to create a function getGemsOfColor
which takes a color string and a gems array as its argument, and returns the names of the gems of that color as an array, sorted alphabetically. If no gems of the given color are found, return an empty array.
Assumptions
- The colors property of each gem is never undefined.
- The gems input array is never empty.
Examples:
Given input array: gems = [
{ name: 'Amethyst', colors: 'purple' },
{ name: 'Garnet', colors: ['orange', 'red']},
{ name: 'Aquamarine', colors: ['blue', 'green']},
{ name: 'Emerald', colors: 'green' },
];
getGemsOfColor('blue', gems) // ['Aquamarine']
getGemsOfColor('green', gems) // ['Aquamarine', 'Emerald']
getGemsOfColor('white', gems) // []
function getGemsOfColor(color, gems) {
}
// 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.
const assert = require("chai").assert;
describe("Sample test cases", function() {
const gems = [
{ name: 'Emerald', colors: 'green' },
{ name: 'Amethyst', colors: 'purple' },
{ name: 'Garnet', colors: ['orange', 'red']},
{ name: 'Aquamarine', colors: ['blue', 'green']},
];
it("Get all blue gems", function() {
assert.strictEqual(getGemsOfColor('blue', gems), ['Aquamarine']);
});
it("Get all green gems", function() {
assert.strictEqual(getGemsOfColor('green', gems), ['Aquamarine', 'Emerald']);
});
it("Get all white gems (empty)", function() {
assert.strictEqual(getGemsOfColor('white', gems), []);
});
});
I suggest first converting the string to lower case so you don't need to add the conditional for checking for the uppercase 'A'.