Skip to content

Instantly share code, notes, and snippets.

@andrit
Created July 12, 2018 16:58
Show Gist options
  • Save andrit/5fc5a12ac1a25158f96b43be147f4890 to your computer and use it in GitHub Desktop.
Save andrit/5fc5a12ac1a25158f96b43be147f4890 to your computer and use it in GitHub Desktop.
function mapConsecutive(values, fn) {
  let result = [];
  for(let i=0; i < values.length - 1; i++){
    result.push(fn(values[i], values[i+1]));
   }
   return result;
}

const letters = ['a', 'b', 'c', 'd', 'e', 'f', 'g'];
let twoByTwo = mapConsecutive(letters, (x,y) => [x,y]);
console.log(twoByTwo);
//[[a,b], [b,c], [c,d], [d,e], [e,f], [f,g]]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment