JavaScript newbie here. I am having problems with the calculator I am trying to build using JavaScript. I was following a tutorial on youtube
Here is the JavaScript Code.
That looks odd to me - how can selecting off of a class work? Since there can be more than one use of a class, that won’t select a single element but I’d expect it to select (if it selected anything) to return and array. But as it turns out, it selects nothing.
I think that if you figure out why those aren’t working, then you’ll be able to move forward on this.
Uncaught TypeError: Cannot set property ‘innerHTML’ of null
It can’t set that property because we are trying to set it on null, which is a primitive and can’t have properties.
It was telling you exactly what the problem was. That’s why it’s important to read the error messages. And why it is important to report them properly for us to help you.
When you specify the css selector pattern, there is a difference between “.operation.value” and “.operation .value”. The second pattern has a space between “.operation” and “.value”. If you don’t use the space, it will look for the first element that has the two classes “operation” and “value”. This is wrong, since I assume you are looking for the element having class “value” and one of its parent elements has the class “operation”.
When I inserted a space between “.operation” and “.value” (same between “.result” and “.value”, then the calculator seemed to work.
Just note that you might want to update the result display after every key press on a number to show the user that the calculator received the input. Currently the result is updated when the calculate button is pressed ("=").