Ad
Algorithms
Arrays
Mathematics
Geometry
Code
Diff
  • makeMove=(c,s)=>[...s].reduce((r,d)=>[r[0]+(d=='r')-(d=='l'),r[1]+(d=='t')-(d=='b')],c)
    • makeMove
    • =(c,s)=>
    • [...s].reduce((r,d)=>[r[0]+(d=='r')-(d=='l'),r[1]+(d=='t')-(d=='b')],c)
    • makeMove=(c,s)=>[...s].reduce((r,d)=>[r[0]+(d=='r')-(d=='l'),r[1]+(d=='t')-(d=='b')],c)
Code
Diff
  • 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;

}