Algorithms
Arrays
Mathematics
Geometry
const firstNonRepeatingCharacter = (str) => { let char=null; console.log(str); str = [...str.toLowerCase()]; str.sort().some((c, i, s)=> { if(s.lastIndexOf(c)==i){ char = c; return char; } s.splice(i, s.lastIndexOf(c)-i); }); return char; };
- const firstNonRepeatingCharacter = (str) => {
let chars = [], counts = [];for(const char of str) {let index = chars.indexOf(char);if(index < 0) {index = counts.length;chars.push(char);counts.push(0);- let char=null; console.log(str);
- str = [...str.toLowerCase()];
- str.sort().some((c, i, s)=> {
- if(s.lastIndexOf(c)==i){
- char = c;
- return char;
- }
counts[index]++;}for(let index = 0; index < chars.length; index++) {if(counts[index] === 1) {return chars[index];}}return null;- s.splice(i, s.lastIndexOf(c)-i);
- });
- return char;
- };
Algorithms
Arrays
Mathematics
Geometry
Description
Implement the makeMove
function that takes initial coordinates and a sequence of movements (e.g. "ttrbrrbllrt"), and returns the new coordinates.
Examples
makeMove([0, 0], "ttrbrrbllrt") // returns [2, 2]
makeMove([5, 5], "trlbrb") // returns [6, 4]
function makeMove(init, sequence) {
[...sequence].forEach(move => {
switch (move) {
case 't':
init[1]++;
break;
case 'b':
init[1]--;
break;
case 'r':
init[0]++;
break;
case 'l':
init[0]--;
break;
default:
break;
}
});
return init;
}
// 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
describe("Solution", function() {
it("should test for something", function() {
assert.deepEqual(makeMove([0, 0], "ttrbrrbllrt"), [2, 1]);
assert.deepEqual(makeMove([4,1], "bllbrt"), [3,0]);
assert.deepEqual(makeMove([-2, 4], "trlbb"), [-2,3]);
assert.deepEqual(makeMove([5,5], "trlbrb"), [6, 4]);
});
});