Intuition

  • iterator_pattern has three variables in the initialisation phase, a pointer with 0 value initially, an array which stores all array values.
  • Keep iterating till there are elements in array
  • As an addition I should be able to add method like map, filter which are actually part of 6. Functional Programming.
function iterateOnMultiples(arr, divisor) {  
 this.cursor = 0;  
 this.array = arr;  
 this.divisor = divisor || 1; 
}  
 
iterateOnMultiples.prototype.next = function() {  
 while (this.cursor <   this.array.length) {  
 var value = this.array[this.cursor++];  
	if (value % this.divisor === 0) {  
	  return value;  
	}  
 }  
};  
iterateOnMultiples.prototype.hasNext = function() {  
var cur = this.cursor;  
	while (cur < this.array.length) {  
	 if (this.array[cur++] %  this.divisor === 0) {  
	  return true;  
	 }  
	}  
return false;  
};
 
var consumer = new iterateOnMultiples([1, 2, 3, 4, 5, 6, 7, 8, 9, 10], 3);  
console.log(consumer.next(), consumer.hasNext()); // 3 true  
console.log(consumer.next(), consumer.hasNext()); // 6 true  
console.log(consumer.next(), consumer.hasNext()); // 9 false