Invalid JS token

Asked

Viewed 47 times

-1

I’m having problems with a calculator design done in JS. On a specific line it says that an unexpected token has been found. I already went to Github of the course and checked their final file, and on the line where in my program (I use Visualcodeestudio) appears the error, in the other program I opened (Sublime) their code, is no error..

Grateful from now on!

class CalcController {

constructor(){
this._locale = 'pt-BR';
this._displayCalcEl = document.querySelector("#display");
this._dateEl = document.querySelector("#data");
this._timeEl = document.querySelector("#hora");
this._currentDate;
this.initialize();
this.initButtonsEvents();

}

initialize(){
   
this.setDisplayDateTime();

setInterval(()=>{

    this.setDisplayDateTime();

}, 1000);


addEventListenerAll(element, events, fn){

        events.split(' ').forEach(event => {

            element.addEventListener(event, fn, false);

        });


}

   




}

initButtonsEvents(){

    let buttons = document.querySelectorAll("#buttons > g, #parts > g") 
        
        buttons.forEach((btn, index)=>{

            this.addEventListenerAll(btn, "click drag", e=>{

                let textBtn = btn.className.baseVal.replace("btn-",""));
        
        
            });

            this.addEventListenerAll(btn, "mouseover mouseup mousedown", e=>{

                btn.style.cursor = "pointer";

            });

        });



}

setDisplayDateTime() {

    this.displayDate = this.currentDate.toLocaleDateString(this._locale,{
        day:"2-digit",
        month: "long",
        year:"numeric"

    });
    this.displayTime = this.currentDate.toLocaleTimeString(this._locale);



}

get displayTime(){
return this._timeEl.innerHTML;

}

set displayTime(valor){
    return this._timeEl.innerHTML = valor;
}


get displayDate(){
return this._dateEl.innerHTML;

}

set displayDate(valor){
    return this._dateEl.innerHTML = valor;
}


get displayCalc(){
    return this._displayCalcEl.innerHTML;
}

set displayCalc(valor){
    this._displayCalcEl.innerHTML = valor;
}

get currentDate(){
    return new Date();
}

set currentDate(valor){
    this._currentDate = valor;
}


}

Print do projeto aberto no Chrome e do console mostrando o erro

1 answer

0


You only get this error in Vscode because it must be configured with Eslint, which helps you in identifying errors in the code. Your error is JS formatting and syntax, so invalid token error.

class CalcController {
  constructor() {
    this._locale = "pt-BR";
    this._displayCalcEl = document.querySelector("#display");
    this._dateEl = document.querySelector("#data");
    this._timeEl = document.querySelector("#hora");
    this._currentDate;
    this.initialize();
    this.initButtonsEvents();
  }

  initialize() {
    this.setDisplayDateTime();

    setInterval(() => {
      this.setDisplayDateTime();
    }, 1000);
  }

  addEventListenerAll(element, events, fn) {
    events.split(" ").forEach(event => {
      element.addEventListener(event, fn, false);
    });
  }

  initButtonsEvents() {
    let buttons = document.querySelectorAll("#buttons > g, #parts > g");

    buttons.forEach((btn, index) => {
      this.addEventListenerAll(btn, "click drag", e => {
        let textBtn = btn.className.baseVal.replace("btn-", "");
      });

      this.addEventListenerAll(btn, "mouseover mouseup mousedown", e => {
        btn.style.cursor = "pointer";
      });
    });
  }

  setDisplayDateTime() {
    this.displayDate = this.currentDate.toLocaleDateString(this._locale, {
      day: "2-digit",
      month: "long",
      year: "numeric"
    });
    this.displayTime = this.currentDate.toLocaleTimeString(this._locale);
  }

  get displayTime() {
    return this._timeEl.innerHTML;
  }

  set displayTime(valor) {
    return (this._timeEl.innerHTML = valor);
  }

  get displayDate() {
    return this._dateEl.innerHTML;
  }

  set displayDate(valor) {
    return (this._dateEl.innerHTML = valor);
  }

  get displayCalc() {
    return this._displayCalcEl.innerHTML;
  }

  set displayCalc(valor) {
    this._displayCalcEl.innerHTML = valor;
  }

  get currentDate() {
    return new Date();
  }

  set currentDate(valor) {
    this._currentDate = valor;
  }
}

Whenever you receive this invalid token error stick to the syntax of writing your code and use Eslint logs to your advantage

  • Okay, I redid the code based on what you gave me, and now the error has changed xD On the Chrome console, "Uncaught Typeerror: this.addEventListenerAll is not a Function at Calccontroller.js:30" appears, meaning this.addeventlistener says it’s not a function.. kkkkk

  • I edited the code, see if it works now

  • I put your code and it miraculously worked! o/ Could you explain to me what was wrong this time? Thanks!

  • Some characters were missing or left over, such as } and ). Put the two codes in parallel and compare, you will notice

Browser other questions tagged

You are not signed in. Login or sign up in order to post.