6 kyu

Create iterator

Description:

Task

Implement an iterator which receives an array of values, and returns an object with:

  • a function next
  • a number index

A call to the next function should return an object with 2 attributes:

  • value (the next value in the input array; undefined if the value is not present or the array has been fully processed)
  • done (boolean which specifies whether the input array has been fully processed)

Accessing the index attribute should return the index of the value currently held by the iterator.


Example:

const iterator = createIterator(['One', , 'Two']);

iterator.index  // 0
iterator.next() // { value: 'One', done: false }
iterator.index  // 1
  
// A hole in the array - value is undefined
iterator.next() // { value: undefined, done: false }
iterator.index  // 2

iterator.next() // { value: 'Two', done: false }
iterator.index  // 3

// Iteration has finished - value is undefined, done becomes true
iterator.next() // { value: undefined, done: true }
iterator.index  // 3

// Subsequent calls to next of a fully processed iterator don't change anything
iterator.next() // { value: undefined, done: true }
iterator.index  // 3
Fundamentals
Arrays

Similar Kata:

Stats:

CreatedFeb 25, 2019
PublishedFeb 25, 2019
Warriors Trained2814
Total Skips13
Total Code Submissions8095
Total Times Completed2206
JavaScript Completions2206
Total Stars22
% of votes with a positive feedback rating89% of 118
Total "Very Satisfied" Votes98
Total "Somewhat Satisfied" Votes15
Total "Not Satisfied" Votes5
Total Rank Assessments11
Average Assessed Rank
6 kyu
Highest Assessed Rank
6 kyu
Lowest Assessed Rank
8 kyu
Ad
Contributors
  • OlegBilykPro Avatar
  • FArekkusu Avatar
Ad