I'm a beginner in javascript but I'm proficient in java. My problem is that I created a class called Trapezium that contains 2 buttons and a value. The goal is for one button to subtract 1 from the value and the other to add 1 to the value. The value after the click should be displayed in the console.
However, when I click either button "NaN" gets displayed in the console. I have no idea why it thinks that my value is not a number. I tried to parseInt the value and that didn't work either. Can someone please explain to me what I am doing wrong?
Here is my code:
class Trapezium {
constructor(element){
this.element = element;
this.decrement = document.createElement("BUTTON");
this.decrement.className = "decrement";
this.decrement.innerHTML = "-";
this.element.appendChild(this.decrement);
var num = 0;
this.increment = document.createElement("BUTTON");
this.increment.className = "increment";
this.increment.innerHTML = "+";
this.element.appendChild(this.increment);
this.increment.addEventListener("click", this.add);
this.decrement.addEventListener("click", this.subtract);
}
add()
{
this.num += 1;
console.log(this.num);
}
subtract()
{
this.num -= 1;
console.log(this.num);
}
}