Play Home Contribute GitHub Join Us Discourse Staff Members GitHub Contribution Guides Team

Magic Exam: JavaScript Generator


#1

In the Magic Exam level, there are 8 locations (marked by X’s) the character must visit.

The y coordinates are one of two values, and the x coordinates increase in a linear fashion.

It would be possible to hardcode an array of objects containing these coordinates, but I thought it would be interesting to try using a generator to produce the coordinates, something like the following:

function* xGenerator() {
    var x = 18;
    var ys = [40, 24];
    var i = 0;
    
    while(true) {
        yield {x: x, y: ys[i++ % 2]};
        if (i % 2 === 0) {
            x += 16;
        }
    }
}

Then by using:

var xs = new xGenerator();
xs.next().value;

We get the coordinates of the X’s.

Unfortunately, this isn’t supported (an Abstract Syntax Tree error is reported), which is unfortunate. I understand that new coders aren’t likely to be using features like generators, but wouldn’t it be better to let people be as creative as they want to be?

Other situations I’ve seen this occur is in the inability to write arrow functions, and also the generation of a warning that functions should not be declared in loops (e.g. inside the main while-true loop, using something like array.map or array.filter passing an anonymous function).

Any idea if these features and others will be supported?


#2

Can you explain about what you are referring to? Like what you are trying to accomplish with this code and the level etc…