7 kyu
Iterator Basics
448matt c
Description:
Iterators
Description
Iterator objects enable custom iteration like CLR IEnumerable or Java Iterable. Generalize for..in to custom iterator-based iteration with for..of. Don’t require realizing an array, enabling lazy design patterns like LINQ.
Task
Your task is simple, create a counter that increments by 1 each time. You must use the iterator described below. Good luck!
Example
let fibonacci = {
[Symbol.iterator]() {
let pre = 0, cur = 1;
return {
next() {
[pre, cur] = [cur, pre + cur];
return { done: false, value: cur }
}
}
}
}
for (let n of fibonacci) {
// truncate the sequence at 1000
if (n > 1000)
break;
console.log(n);
}
Reading: Iterators and Generators
Iterators
Fundamentals
Similar Kata:
Stats:
Created | Jul 22, 2015 |
Published | Jul 22, 2015 |
Warriors Trained | 1184 |
Total Skips | 149 |
Total Code Submissions | 1199 |
Total Times Completed | 448 |
JavaScript Completions | 448 |
Total Stars | 15 |
% of votes with a positive feedback rating | 79% of 87 |
Total "Very Satisfied" Votes | 56 |
Total "Somewhat Satisfied" Votes | 26 |
Total "Not Satisfied" Votes | 5 |