Week 13
Quiz 35a

Assignment: Iterator of odd numbers

Given the Iterator constructor as in the lectures:

const Iterator = (startValue, whileFn, incrementFn) => {
    const next = () => {
        const proceed = whileFn(startValue);
        let value = undefined;
        if (proceed) {
            value = startValue;
            startValue = incrementFn(startValue);
        }
        return {value: value, done: !proceed};
    };
    return {
        [Symbol.iterator]: () => ({next})
    };
};

provide code like

const startValue  = ____;
const whileFn     = ____;
const incrementFn = ____;

such that Iterator(startValue, whileFn, incrementFn); produces all odd numbers between 10 and 20.

Solution
const startValue  = 11;
const whileFn     = v => v < 20;
const incrementFn = v => v + 2;

Your solution will be tested against:

const Iterator = (startValue, whileFn, incrementFn) => {
    const next = () => {
        const proceed = whileFn(startValue);
        let value = undefined;
        if (proceed) {
            value = startValue;
            startValue = incrementFn(startValue);
        }
        return {value: value, done: !proceed};
    };
    return {
        [Symbol.iterator]: () => ({next})
    };
};
 
[...Iterator(startValue, whileFn, incrementFn)].toString() === "11,13,15,17,19"