Skip to content

Instantly share code, notes, and snippets.

@saintsGrad15
Created July 18, 2019 21:13
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save saintsGrad15/afd1d420a22af1cdd3908632c780b374 to your computer and use it in GitHub Desktop.
Save saintsGrad15/afd1d420a22af1cdd3908632c780b374 to your computer and use it in GitHub Desktop.
A generator function that returns an Array consisting of [index, element]. Behaves like the Python function.
function* enumerate(array)
{
/**
* Duplicates the functionality of Python's enumerate function.
*
* Return, for each element of 'array' an array the element's index and the element.
*
* e.g.
*
* [i, elem]
*
* :param: array: (Array) Any array.
*
* :yield: For each element of 'array' this generator function will yield an Array of the form
* [<element index≥, element]
*/
let counter = 0;
for (let element of array)
yield [counter++, element];
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment