same method but shorter
n=s=>[...s].map(e=>("aeiou".includes(e)?"aeiou".indexOf(e)+1:e)).join('') d=s=>[...s].map(e=>("12345".includes(e)?"aeiou"[e-1]:e)).join('')
function encode(string){return [...string].map(el => {return ( "aeiou".includes(el))? "aeiou".indexOf(el) + 1 : el}).join('')}function decode(string){return [...string].map(el => {return ("12345".includes(el)) ? {'1':"a","2":"e","3":"i","4":"o","5":"u"}[el] : el}).join('')}- n=s=>[...s].map(e=>("aeiou".includes(e)?"aeiou".indexOf(e)+1:e)).join('')
- d=s=>[...s].map(e=>("12345".includes(e)?"aeiou"[e-1]:e)).join('')
// Since Node 10, we're using Mocha. // You can use `chai` for assertions. const chai = require("chai"); const assert = chai.assert; // 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 encode vowels correctly", function() { assert.strictEqual(n("hello"), "h2ll4"); assert.strictEqual(n("codewars"), "c4d2w1rs"); assert.strictEqual(n("a"), "1"); assert.strictEqual(n("e"), "2"); assert.strictEqual(n("i"), "3"); assert.strictEqual(n("o"), "4"); assert.strictEqual(n("u"), "5"); }); it("should leave consonants unchanged in encode", function() { assert.strictEqual(n("bcdfghjklmnpqrstvwxyz"), "bcdfghjklmnpqrstvwxyz"); }); it("should decode numbers to vowels correctly", function() { assert.strictEqual(d("h2ll4"), "hello"); assert.strictEqual(d("c4d2w1rs"), "codewars"); assert.strictEqual(d("1"), "a"); assert.strictEqual(d("2"), "e"); assert.strictEqual(d("3"), "i"); assert.strictEqual(d("4"), "o"); assert.strictEqual(d("5"), "u"); }); it("should leave other numbers and consonants unchanged in decode", function() { assert.strictEqual(d("bcdfghjklmnpqrstvwxyz"), "bcdfghjklmnpqrstvwxyz"); assert.strictEqual(d("6789"), "6789"); }); it("should handle empty strings", function() { assert.strictEqual(n(""), ""); assert.strictEqual(d(""), ""); }); it("should handle strings with mixed vowels and consonants", function() { assert.strictEqual(n("hello world"), "h2ll4 w4rld"); assert.strictEqual(d("h2ll4 w4rld"), "hello world"); }); it("should handle strings with numbers", function() { assert.strictEqual(n("h2ll4"), "h2ll4"); assert.strictEqual(d("c4d2w1rs123"), "codewarsaei"); }); });
- // Since Node 10, we're using Mocha.
- // You can use `chai` for assertions.
- const chai = require("chai");
- const assert = chai.assert;
- // 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 something", function() {// Test.assertEquals(1 + 1, 2);// assert.strictEqual(1 + 1, 2);- it("should encode vowels correctly", function() {
- assert.strictEqual(n("hello"), "h2ll4");
- assert.strictEqual(n("codewars"), "c4d2w1rs");
- assert.strictEqual(n("a"), "1");
- assert.strictEqual(n("e"), "2");
- assert.strictEqual(n("i"), "3");
- assert.strictEqual(n("o"), "4");
- assert.strictEqual(n("u"), "5");
- });
- it("should leave consonants unchanged in encode", function() {
- assert.strictEqual(n("bcdfghjklmnpqrstvwxyz"), "bcdfghjklmnpqrstvwxyz");
- });
- it("should decode numbers to vowels correctly", function() {
- assert.strictEqual(d("h2ll4"), "hello");
- assert.strictEqual(d("c4d2w1rs"), "codewars");
- assert.strictEqual(d("1"), "a");
- assert.strictEqual(d("2"), "e");
- assert.strictEqual(d("3"), "i");
- assert.strictEqual(d("4"), "o");
- assert.strictEqual(d("5"), "u");
- });
- it("should leave other numbers and consonants unchanged in decode", function() {
- assert.strictEqual(d("bcdfghjklmnpqrstvwxyz"), "bcdfghjklmnpqrstvwxyz");
- assert.strictEqual(d("6789"), "6789");
- });
- it("should handle empty strings", function() {
- assert.strictEqual(n(""), "");
- assert.strictEqual(d(""), "");
- });
- it("should handle strings with mixed vowels and consonants", function() {
- assert.strictEqual(n("hello world"), "h2ll4 w4rld");
- assert.strictEqual(d("h2ll4 w4rld"), "hello world");
- });
- it("should handle strings with numbers", function() {
- assert.strictEqual(n("h2ll4"), "h2ll4");
- assert.strictEqual(d("c4d2w1rs123"), "codewarsaei");
- });
- });
needs little work LOL
r=(a,b)=>a==b?'Draw':`Player ${((a.charCodeAt(3)-b.charCodeAt(3))/3 > 0)+1} wins`
dumbRockPaperScissors=(a,b)=>a==b?`Draw`:`Player ${(a.slice(0,1)!={'R':'P','P':'S','S':'R'}[b.slice(0,1)])+1} wins`- r=(a,b)=>a==b?'Draw':`Player ${((a.charCodeAt(3)-b.charCodeAt(3))/3 > 0)+1} wins`
// Since Node 10, we're using Mocha. // You can use `chai` for assertions. const chai = require("chai"); const assert = chai.assert; // 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('r', function() { it('returns "Draw" for Rock vs Rock', function() { assert.strictEqual(r('Rock', 'Rock'), 'Draw'); }); it('returns "Draw" for Paper vs Paper', function() { assert.strictEqual(r('Paper', 'Paper'), 'Draw'); }); it('returns "Draw" for Scissors vs Scissors', function() { assert.strictEqual(r('Scissors', 'Scissors'), 'Draw'); }); it('returns "Player 1 wins" for Rock vs Scissors', function() { assert.strictEqual(r('Rock', 'Scissors'), 'Player 1 wins'); }); it('returns "Player 1 wins" for Scissors vs Paper', function() { assert.strictEqual(r('Scissors', 'Paper'), 'Player 1 wins'); }); it('returns "Player 1 wins" for Paper vs Rock', function() { assert.strictEqual(r('Paper', 'Rock'), 'Player 1 wins'); }); it('returns "Player 2 wins" for Scissors vs Rock', function() { assert.strictEqual(r('Scissors', 'Rock'), 'Player 2 wins'); }); it('returns "Player 2 wins" for Paper vs Scissors', function() { assert.strictEqual(r('Paper', 'Scissors'), 'Player 2 wins'); }); it('returns "Player 2 wins" for Rock vs Paper', function() { assert.strictEqual(r('Rock', 'Paper'), 'Player 2 wins'); }); });
- // Since Node 10, we're using Mocha.
- // You can use `chai` for assertions.
- const chai = require("chai");
- const assert = chai.assert;
- // 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('dumbRockPaperScissors', function() {- describe('r', function() {
- it('returns "Draw" for Rock vs Rock', function() {
assert.strictEqual(dumbRockPaperScissors('Rock', 'Rock'), 'Draw');- assert.strictEqual(r('Rock', 'Rock'), 'Draw');
- });
- it('returns "Draw" for Paper vs Paper', function() {
assert.strictEqual(dumbRockPaperScissors('Paper', 'Paper'), 'Draw');- assert.strictEqual(r('Paper', 'Paper'), 'Draw');
- });
- it('returns "Draw" for Scissors vs Scissors', function() {
assert.strictEqual(dumbRockPaperScissors('Scissors', 'Scissors'), 'Draw');- assert.strictEqual(r('Scissors', 'Scissors'), 'Draw');
- });
- it('returns "Player 1 wins" for Rock vs Scissors', function() {
assert.strictEqual(dumbRockPaperScissors('Rock', 'Scissors'), 'Player 1 wins');- assert.strictEqual(r('Rock', 'Scissors'), 'Player 1 wins');
- });
- it('returns "Player 1 wins" for Scissors vs Paper', function() {
assert.strictEqual(dumbRockPaperScissors('Scissors', 'Paper'), 'Player 1 wins');- assert.strictEqual(r('Scissors', 'Paper'), 'Player 1 wins');
- });
- it('returns "Player 1 wins" for Paper vs Rock', function() {
assert.strictEqual(dumbRockPaperScissors('Paper', 'Rock'), 'Player 1 wins');- assert.strictEqual(r('Paper', 'Rock'), 'Player 1 wins');
- });
- it('returns "Player 2 wins" for Scissors vs Rock', function() {
assert.strictEqual(dumbRockPaperScissors('Scissors', 'Rock'), 'Player 2 wins');- assert.strictEqual(r('Scissors', 'Rock'), 'Player 2 wins');
- });
- it('returns "Player 2 wins" for Paper vs Scissors', function() {
assert.strictEqual(dumbRockPaperScissors('Paper', 'Scissors'), 'Player 2 wins');- assert.strictEqual(r('Paper', 'Scissors'), 'Player 2 wins');
- });
- it('returns "Player 2 wins" for Rock vs Paper', function() {
assert.strictEqual(dumbRockPaperScissors('Rock', 'Paper'), 'Player 2 wins');- assert.strictEqual(r('Rock', 'Paper'), 'Player 2 wins');
- });
- });
that's called "thinking outside the box"
dumbRockPaperScissors=(a,b)=>a==b?`Draw`:`Player ${(a.slice(0,1)!={'R':'P','P':'S','S':'R'}[b.slice(0,1)])+1} wins`
dumbRockPaperScissors=(a,b)=>a==b?`Draw`:`Player ${(a!={'Rock':'Paper','Paper':'Scissors','Scissors':'Rock'}[b])+1} wins`- dumbRockPaperScissors=(a,b)=>a==b?`Draw`:`Player ${(a.slice(0,1)!={'R':'P','P':'S','S':'R'}[b.slice(0,1)])+1} wins`
Strings
Can't beat this can you? 😏
m=x=>['codewarz','abracadabra!'][+x];
const magick_message = x => ['codewarz', 'abracadabra!'][+x];- m=x=>['codewarz','abracadabra!'][+x];
const Test = require('@codewars/test-compat'); describe('Example', function() { it('test case', function() { Test.assertEquals(m(true), 'abracadabra!') Test.assertEquals(m(false), 'codewarz') }); });
- const Test = require('@codewars/test-compat');
- describe('Example', function() {
- it('test case', function() {
Test.assertEquals(magick_message(true), 'abracadabra!')Test.assertEquals(magick_message(false), 'codewarz')- Test.assertEquals(m(true), 'abracadabra!')
- Test.assertEquals(m(false), 'codewarz')
- });
- });
const areAnagrams = (word1, word2) => {
const clean = str => str.replace(/\s/g, '').toLowerCase();
const count = str => Array.from(clean(str)).reduce((map, char) => map.set(char, (map.get(char) || 0) + 1), new Map());
const map1 = count(word1);
const map2 = count(word2);
return Array.from(map1.keys()).every(char => map1.get(char) === map2.get(char));
};
// 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:
// const Test = require("@codewars/test-compat");
describe("Anagram Detector", function () {
it("should return true for anagrams", function () {
Test.assertEquals(areAnagrams("listen", "silent"), true);
Test.assertEquals(areAnagrams("Dormitory", "dirty room"), true);
Test.assertEquals(areAnagrams("The Morse Code", "Here come dots"), true);
Test.assertEquals(areAnagrams("Astronomer", "Moon starer"), true);
});
it("should return false for non-anagrams", function () {
Test.assertEquals(areAnagrams("hello", "world"), false);
Test.assertEquals(areAnagrams("abc", "def"), false);
});
it("should handle spaces and case insensitivity", function () {
Test.assertEquals(areAnagrams("Astronomer", "moon starer"), true);
});
});
Fundamentals
Strings
Fundamentals
Strings
there's a shorter way, but same steps as my previous kumite.
Fundamentals
Strings
transform array to string by .split("")
, reverse it then make it string again by .join("")