Find your hat project javascript

I think alls is good in my code,then I try to run my code in bash terminal that’s something goes wrong it execute this error :
SyntaxError: Invalid or unexpected token
at createScript (vm.js:53:10)
at Object.runInThisContext (vm.js:95:10)
at Module._compile (module.js:543:28)
at Object.Module._extensions…js (module.js:580:10)
at Module.load (module.js:488:32)
at tryModuleLoad (module.js:447:12)
at Function.Module._load (module.js:439:3)
at Module.runMain (module.js:605:10)
at run (bootstrap_node.js:427:7)
at startup (bootstrap_node.js:151:9)`Preformatted text``
thos is my code:

const prompt = require('prompt-sync')({sigint: true});

	
const hat = '^';
const hole = 'O';
const fieldCharacter = '░';
const pathCharacter = '*';
​
class Field {
constructor(field) {
this.field = field;
this.locationY = 0;
this.locationX = 0;
}
 
  runGame() {
  let playing = true;
    while(playing) {
    this.print();
    this.instruction();
    if(this.field[this.locationY][this.locationX] == hole) {
    console.log("You loses.You had fall in hole");
      playing = false;
      break;
    } else if(this.field[this.locationY][this.locationX] == hat) {
    console.log("Congrats.You wins");
      playing = false;
      break;
    } else if(!this.isInBounds()) {
    console.log("You are out side of field's border");
      playing = false;
      break;
    }
      this.field[this.locationY][this.locationX] = pathCharacter;
    }
    
  }
  isInBounds(){
    return(
      this.locationY >= 0 && this.locationX >= 0 && this.locationY < this.field.length && this.locationX < this.field[0].length;
    )
  }
   print() {
  const bigField = this.field.map(raw => {
  raw.join('')}).join('\n');
    console.log(bigField);
  }
  
  instruction() {
 const answer = prompt('Which ways?').toUpperCase();
    switch(answer) {
        case'U':
        this.locationY -= 1;
        break;
        case'D':
        this.locationY += 1;
        break;
        case'R':
        this.locationX += 1;
        break;
        case'L':
        this.locationX -= 1;
        break;
      default:
        console.log("Choose one of the four directions 'U', 'D', 'R' or 'L' ");
        this.instruction();
        break;
    }
  }
 static generateField(h, w, holePercent) 
  {
​
    let field = [];
  for(let i = 0; i < h; i++) {
    field.push([]);
    
      for(let j = 0; j < w; j++) {
       
      const prob = Math.random();
      if(prob > holePercent) {
       field[i][j] = fieldCharacter;
      } else {
        field[i][j] = hole;
      }
    }
  
  }
​
    const hatY = Math.floor(Math.random() * h);
  const hatX = Math.floor(Math.random() * w);
     
   while(hatY == 0 && hatX == 0) {
    hatY = Math.floor(Math.random() * h);
     hatX = Math.floor(Math.random() * w);
  }
    field[0][0] = pathCharacter;
   field[hatY][hatX] = hat; 
    return field;
 }
}
​
let myField = new Field(Field.generateField(5, 4 , 0.2));
myField.runGame();

```[https://www.codecademy.com/paths/full-stack-engineer-career-path/tracks/fscp-javascript-syntax-part-iii/modules/fecp-challenge-project-find-your-hat/projects/find-your-hat](https://www.codecademy.com/paths/full-stack-engineer-career-path/tracks/fscp-javascript-syntax-part-iii/modules/fecp-challenge-project-find-your-hat/projects/find-your-hat)

Hi @method3884535642

const hatY = Math.floor(Math.random() * h);
  const hatX = Math.floor(Math.random() * w);
     
   while(hatY == 0 && hatX == 0) {
    hatY = Math.floor(Math.random() * h);
     hatX = Math.floor(Math.random() * w);
  }

You cannot reassign a const.

Try to move the semicolon behind the closing parenthesis.

i make a semicolon behind the parenthesis and replace a const by let and it’s the same thing

When I do that I can play the game.

it weird it don’t work with me

This topic was automatically closed 41 days after the last reply. New replies are no longer allowed.