marijnh / Eloquent-JavaScript

The sources for the Eloquent JavaScript book
https://eloquentjavascript.net
3.01k stars 795 forks source link

Chatper 16 - Platform Game Error - "Uncaught (in promise) TypeError: plan.trim is not a function" #361

Closed PolarisTLX closed 6 years ago

PolarisTLX commented 6 years ago

I first tried writing out the code, and got the error below. Then tried just copying the code straight from the chapter's page and get the same error:

"Uncaught (in promise) TypeError: plan.trim is not a function" pointing to the following parts of the code:

at new Level:

class Level {
  constructor(plan) {
    let rows = plan.trim().split("\n").map(r => r.split(""));
    this.height = rows.length;

at runGame:

async function runGame(plans, Display) {
  for (let level = 0; level < plans.length;) {
    let status = await runLevel(new Level(plans[level]),
                                Display);
    if (status == "won") level++;

at:

runGame(GAME_LEVELS, DOMDisplay);

I am able to use the code from the sandbox, and I can get that to work and play the game, but that seems to only be the ES5 (2nd Edition) versions of the code. So I am a bit lost on how to debug further for the 3rd Edition of the code.

I have tried putting all the code into 1 HTML file (except the CSS) and attached in case that helps. Thank you.

Project 3 - All code in HTML.txt

marijnh commented 6 years ago

You are using the game levels from the 2nd edition, which have a different format. This is rather confusing at the moment, since there are links in the book that will take you to the 2nd edition code sandbox. I hope to move the 3rd edition to the top of the domain, which will fix those links, somewhere next week. For now, get the game levels from http://eloquentjavascript.net/3rd_edition/code/#16