hcodebr / curso-javascript-projeto-calculadora-clone

138 stars 212 forks source link

Aula 14 - calculadora hcode - array não faz o push #9

Closed silvadias closed 3 years ago

silvadias commented 3 years ago

Meu array: addOperation(value){
this._operation.push(value); console.log(this._operation); } Não fez o push para o array. Acredito que meu código esta igual a aula número 14, porem o value deveria vir de initButtonsEvents(). Este não deu certo. Tentei, revisei, as aulas, igual.. talvez tenha esquecido ou feito algo errado quem puder ajudar. vlw

`

class CalcController{ constructor(){ this._locale='pt-BR'; this._operation=[];
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);
};

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

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

clearAll(){

   this._operation=[];
}
clearEntry(){
    this._operation.pop();
}
setError(){
    this.displayCalc="Error";
}

 addOperation(value){       
    this._operation.push(value); 
    console.log(this._operation);
}

execBtn(value){
    switch(value){
        case 'ac':
            this.clearAll();
            break;
        case 'c':
            this.clearEntry();
             break;
        case 'soma':

            break;
        case 'subtracao':

            break;
        case 'divisao':

            break;
        case 'multiplicacao':

            break;
        case 'porcento':

            break;
        case 'igual':

            break;

        case '0':
        case '1':
        case '2':
        case '3':
        case '4':
        case '5':
        case '6':
        case '7':
        case '8':
        case '9':
            this.addOperation(parseInt(value));
            break;
         default:
            this.setError();
            break;
    }
}

initButtonsEvents(){
    let buttons=document.querySelectorAll("#buttons > g, #parts > g");
    buttons.forEach((btn,index)=>{
        this.addEventListenerAll(btn, "click drag mouseover", e => {
            console.log(btn.className.baseVal.replace("btn-",""));
        });
        this.addEventListenerAll(btn, "mouseover mouseup mousedown",e=>{
            let textBtn=btn.style.cursor="pointer";
            this.execBtn(textBtn);
        });
    });
}

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

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

}
get displayDate(){

    return this._dateEl.innerHTML;
}

set displayDate(date){

    return this._dateEl.innerHTML=date;
}

get displayCalc(){
    return this._displayCalcEl.innerHTML; 
}
set displayCalc(valor){
    this._displayCalcEl.innerHTML=valor;
}
get currentDate(){
    return  new Date();
}
set currentDate(data){
    this._currentDate=data;
}

}

`

silvadias commented 3 years ago

kkk Montei errado uma parte, deu certo agora, no final do dia no penalt kkk resposta: initButtonsEvents(){ let buttons=document.querySelectorAll("#buttons > g, #parts > g"); buttons.forEach((btn,index)=>{ this.addEventListenerAll(btn, "click drag mouseover", e => { let textBtn=btn.className.baseVal.replace("btn-",""); this.execBtn(textBtn); }); this.addEventListenerAll(btn, "mouseover mouseup mousedown",e=>{ btn.style.cursor="pointer";

        });
    });
}
silvadias commented 3 years ago

segue Aula 14 para quem agarrou:

` class CalcController{ constructor(){ this._locale='pt-BR'; this._operation=[];
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);
};

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

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

clearAll(){

   this._operation=[];
}
clearEntry(){
    this._operation.pop();
}
setError(){
    this.displayCalc="Error";
}

 addOperation(value){       
    this._operation.push(value); 
    console.log(this._operation);
}

execBtn(value){
    switch(value){
        case 'ac':
            this.clearAll();
            break;
        case 'c':
            this.clearEntry();
             break;
        case 'soma':

            break;
        case 'subtracao':

            break;
        case 'divisao':

            break;
        case 'multiplicacao':

            break;
        case 'porcento':

            break;
        case 'igual':

            break;

        case '0':
        case '1':
        case '2':
        case '3':
        case '4':
        case '5':
        case '6':
        case '7':
        case '8':
        case '9':
            this.addOperation(parseInt(value));
            break;
         default:
            this.setError();
            break;
    }
}

initButtonsEvents(){
    let buttons=document.querySelectorAll("#buttons > g, #parts > g");
    buttons.forEach((btn,index)=>{
        this.addEventListenerAll(btn, "click drag mouseover", e => {
            let textBtn=btn.className.baseVal.replace("btn-","");
            this.execBtn(textBtn); 
        });
        this.addEventListenerAll(btn, "mouseover mouseup mousedown",e=>{
            btn.style.cursor="pointer";

        });
    });
}

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

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

}
get displayDate(){

    return this._dateEl.innerHTML;
}

set displayDate(date){

    return this._dateEl.innerHTML=date;
}

get displayCalc(){
    return this._displayCalcEl.innerHTML; 
}
set displayCalc(valor){
    this._displayCalcEl.innerHTML=valor;
}
get currentDate(){
    return  new Date();
}
set currentDate(data){
    this._currentDate=data;
}

} `

silvadias commented 3 years ago

Achei o erro