Trimmed the lambda
function getGemsOfColor(color, gems) { return gems.filter(gem => gem.colors.includes(color)).map(it => it.name).sort(); }
- function getGemsOfColor(color, gems) {
return gems.filter(gem => gem.colors.includes(color)).map(it => {return it.name}).sort();- return gems.filter(gem => gem.colors.includes(color)).map(it => it.name).sort();
- }
What's the most efficient algorithm to find the prime divisors of a number?
function divisors(x) {
if (x === 1) return [];
let sqrt = Math.sqrt(x);
let res = [];
while (x % 2 === 0) {
x /= 2;
res.push(2);
}
for (let n = 3; n <= sqrt && x > 1; n += 2) {
if (x % n === 0) {
x /= n;
sqrt = Math.sqrt(x);
res.push(n);
n -= 2;
}
}
if (x > 1) res.push(x);
return res;
}
// 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.
describe("Basic checks", function() {
it("Does the function work?", function() {
Test.assertDeepEquals(divisors(1), []);
Test.assertDeepEquals(divisors(2), [2]);
Test.assertDeepEquals(divisors(3), [3]);
Test.assertDeepEquals(divisors(4), [2, 2]);
Test.assertDeepEquals(divisors(12), [2, 2, 3]);
});
});
describe("Bigger numbers", function() {
it("Will it stand them?", function() {
Test.assertDeepEquals(divisors(485690777622330), [2, 3, 5, 7, 7, 17, 31, 31, 43, 47, 10007]);
Test.assertDeepEquals(divisors(6469693230), [2, 3, 5, 7, 11, 13, 17, 19, 23, 29]);
Test.assertDeepEquals(divisors(114909829), [10453, 10993]);
});
});
describe("Random tests", () => {
it("Hardcoding-spoof", () => {
let primes = [2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31];
for (let n = 4; n < 10; n++) {
let arr = [];
let product = 1;
for (let o = 0; o < n; o++) {
let value = primes[Math.floor(Math.random()*primes.length)]
arr.push(value);
product *= value;
}
arr = arr.sort((a, b) => a - b);
Test.assertDeepEquals(divisors(product), arr);
}
});
});
Using the bitwise not operator; it will only work with integers.
neg = _ => ~_ + 1 // this only works with integers
function neg(number) {return -(number);}- neg = _ => ~_ + 1
- // this only works with integers
// 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: Test.assertEquals(neg(1), -1) Test.assertEquals(neg(3), -3) Test.assertEquals(neg(-7), 7)
- // 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:
Test.assertEquals(neg(1), -1,)- Test.assertEquals(neg(1), -1)
- Test.assertEquals(neg(3), -3)
- Test.assertEquals(neg(-7), 7)
This is just to demonstrate how to do this without regexp, yet in a short way.