Reputation: 91
I have been following a video tutorial for creating a calculator, I have not completed the program yet, but I should be able to type multiple numbers on the calculator then pressing the buttons (without any addition, subtraction, multiplication or division, yet) but instead I get error messages, which I'm not sure how to fix. Can anyone help?
Upvotes: 0
Views: 51
Reputation: 2060
Dan, you should always have a look at the errors and try to figure out what's going on. Your error says: Uncaught TypeError: Cannot read property 'toString' of undefined
Line: 22
Which means, 'hey, something that you try to call toString
on is actually not something like an object or value, but it is undefined
.
Now you should go to line 22, which is:
this.currentOperand = this.currentOperand.toString() + number.toString();
And try to figure out what is wrong - usually you use debugger for it, or just console.log, like this:
appendNumber(number) {
console.log('Current operand: ', this.currentOperand);
console.log('Number: ', number);
this.currentOperand = this.currentOperand.toString() + number.toString();
}
In your case - this.currentOperand
is undefined
, since you never initialise it (it assigned in calculator.clear
, but you never call it).
This fix will make you able to continue:
const calculator = new Calculator(previousOperandTextElement, currentOperandTextElement);
calculator.clear()
Upvotes: 4