Closures
Basic Language Features
Fundamentals
ES2015
Babel
Functions
Control Flow
const unknown = 'Unknown error'
const catalog = {
en: {
ERROR_USER_NOT_FOUND: 'Error: User not found',
ERROR_500: 'Internal Server Error',
},
ru: {
ERROR_USER_NOT_FOUND: 'Ошибка: Пользователь не найден',
}
}
const catalogProvider = locale => error => catalog[locale][error] || unknown
// Since Node 10, we're using Mocha.
// You can use `chai` for assertions.
const chai = require("chai");
const assert = chai.assert;
const Test = require("@codewars/test-compat");
// Uncomment the following line to disable truncating failure messages for deep equals, do:
// chai.config.truncateThreshold = 0;
// Since Node 12, we no longer include assertions from our deprecated custom test framework by default.
// Uncomment the following to use the old assertions:
describe("Solution with EN locale", function() {
const cp = catalogProvider('en');
it("should test for something", function() {
Test.assertEquals( cp('ERROR_USER_NOT_FOUND'), 'Error: User not found');
Test.assertEquals( cp('ERROR_500'), 'Internal Server Error');
// Test.assertEquals(1 + 1, 2);
// assert.strictEqual(1 + 1, 2);
});
});
describe("Solution with RU locale", function() {
const cp = catalogProvider('ru');
it("should test for something", function() {
Test.assertEquals( cp('ERROR_USER_NOT_FOUND'), 'Ошибка: Пользователь не найден');
Test.assertEquals( cp('ERROR_500'), 'Unknown error');
// Test.assertEquals(1 + 1, 2);
// assert.strictEqual(1 + 1, 2);
});
});
Closures
Basic Language Features
Fundamentals
Mathematics
Algorithms
Logic
Numbers
Data Types
- This function takes colors as arguments in the only RGBA
- @param {String} positiveColor
- @param {String} negativeColor
- @param {String} neutralColor
- @return {Function} Which takes a percentage as an argument and returns the color depending on the percentage passed
const calculateColor = ({ positive: positiveColor, negative: negativeColor, neutral: neutralColor }) => {
return percent => {
const absoluteValueOfNumber = Math.sign(percent)
const startPercent = 100
const opacityNeutral = 0.5
const opacityMinimal = 0.25
const isOpacityMinimal = val => val <= opacityMinimal
if (absoluteValueOfNumber === 1) {
const opacity = percent / startPercent
if (isOpacityMinimal(opacity)) {
return assignOpacity(positiveColor, opacityMinimal)
}
return assignOpacity(positiveColor, opacity)
}
if (absoluteValueOfNumber === -1) {
const opacity = Math.abs(percent) / startPercent
if (isOpacityMinimal(opacity)) {
return assignOpacity(negativeColor, opacityMinimal)
}
return assignOpacity(negativeColor, opacity)
}
if (absoluteValueOfNumber === 0) {
return assignOpacity(neutralColor, opacityNeutral)
}
}
}
function assignOpacity (color, value) {
const colorArr = color.split(',').reverse()
const opacity = colorArr[0]
const bracket = opacity[opacity.length - 1]
colorArr[0] = value + bracket
return colorArr.reverse().join()
}
// Since Node 10, we're using Mocha.
// You can use `chai` for assertions.
const Test = require("@codewars/test-compat");
const chai = require("chai");
const assert = chai.assert;
const colors = {
positive: 'rgba(178, 245, 211, 1)',
negative: 'rgba(255, 170, 170, 1)',
neutral: 'rgba(255, 255, 255, 1)',
}
const cc = calculateColor(colors);
// Uncomment the following line to disable truncating failure messages for deep equals, do:
// chai.config.truncateThreshold = 0;
// Since Node 12, we no longer include assertions from our deprecated custom test framework by default.
// Uncomment the following to use the old assertions:
// const Test = require("@codewars/test-compat");
describe("Solution", function() {
it("should test for check colors", function() {
Test.assertEquals( cc(16), 'rgba(178, 245, 211,0.25)');
Test.assertEquals( cc(50), 'rgba(178, 245, 211,0.5)');
Test.assertEquals( cc(-50), 'rgba(255, 170, 170,0.5)');
// Test.assertEquals(1 + 1, 2);
// assert.strictEqual(1 + 1, 2);
});
});