Regarding this question, OP has stated his code works under SpiderMonkey 1.8.5, but not in NodeJS. I've looked at the SpiderMonkey 1.8.5 page and it states that;
Note that version 1.8.5 is outdated. The latest version of SpiderMonkey can always be found on the source download page.
In an effort to try and correctly review this code, I usually first copy the original code onto my machine and create test cases to replicate the expected functionality. That way, once I refactor the code into a 'nicer' form, I can be sure the code still works.
However, I tried to run this code on IoJS v2.3.4, which is an implementation of the v8 engine that supports ES6, in particular, it supports generators (which is the use of the yield
keyword in tandem with function*
to create potentially infinite sequences).
Under this environment, running the code results in a syntax error:
/home/dan/code-review/96661-project-euler-52-permuted-multiples/original.js:9
yield start;
^^^^^
SyntaxError: Unexpected identifier
at exports.runInThisContext (vm.js:53:16)
at Module._compile (module.js:413:25)
at Object.Module._extensions..js (module.js:448:10)
at Module.load (module.js:355:32)
at Function.Module._load (module.js:310:12)
at Function.Module.runMain (module.js:471:10)
at startup (node.js:117:18)
at node.js:948:3
In ES5 (and lower versions, which is what SpiderMonkey runs), the yield
keyword is considered a reserved word, and any ES5-compliant JavaScript engines should raise the error Unexpected reserved word: yield
. And in the ES6 engine - which is when the yield
keyword came into standard parlance, I'm getting a SyntaxError: Unexpected identifier
. Indeed, the latest stable release of node (node v0.12.7) gives us a similar error:
dan@dan-BB-35708A:~/code-review/96661-project-euler-52-permuted-multiples$ nvm i stable
######################################################################## 100.0%
Now using node v0.12.7 (npm v2.11.3)
/home/dan/code-review/96661-project-euler-52-permuted-multiples/original.js:9
yield start;
^^^^^
SyntaxError: Unexpected identifier
at exports.runInThisContext (vm.js:73:16)
at Module._compile (module.js:443:25)
at Object.Module._extensions..js (module.js:478:10)
at Module.load (module.js:355:32)
at Function.Module._load (module.js:310:12)
at Function.Module.runMain (module.js:501:10)
at startup (node.js:129:16)
at node.js:814:3
As far as I am concerned, this question has non-working code, but OP states it does work (for him) on SpiderMonkey 1.8.5. Should we allow this question because it works on a particular version of a browser engine (one I am convinced has a bug because it's not standards compliant given its treatment of yield
) or should it be closed due to non-working code?
Interesting, I've just tested this code on SpiderMonkey 2.4.0 (only version I can seem to find on the Ubuntu package repository) and the code does actually work.
dan@dan-BB-35708A:~/code-review/96661-project-euler-52-permuted-multiples$ js24 original.js
142857