I am a beginner in JavaScript and I am currently working on the basic projects such as calculator, weather app, etc. I had trouble understanding the function where the output is shown on the screen. I have the following function and code:
The Function that outputs the user input and the result:
currentResult is a number. It’s not an object. And it doesn’t have a property textContent
userInput isn’t defined anywhere, so getUserInput is running, at best, if it doesn’t error, parseInt(null), which returns NaN (null is not a number).
So
enteredNumber is NaN. calcDescription is "0 + NaN". currentResult is NaN or an error.
Then you are trying to pass to the function outputResult the sum of NaN and the string "0 + NaN". However, the + will concatenate rather than add, so you’re passing the single argument "NaN0 + NaN". The function is supposed to take two arguments (a number and a string).
Again, it’ll probably have errored by this point anyway.
You are then running this add function when someone clicks the add button.
You need:
A reference to the HTML element you are rendering the result to, saved as a variable. This will not change, you don’t need to overwrite it, get it at the top of your program.
A reference to the HTML element you are rendering the text to. Same thing as above.
These are objects. And they have a textContent property. Then you need:
An element or elements that have the numbers you need.
In the functions that trigger on clicks, you need to get those numbers.
Then you have actual numbers, which you can then use to write the result/text to the elements 1 & 2
There may be some code missing from your example. If that’s the case, apologies for mentioning stuff you’ve already tried to include. If possible, post a link to codepen or whatever, it’s easier to debug