I'm beginning small projects with javascript and I'm not sure why I'm blocked, now.
I'm trying to build a calculator with the little knowledge I have (which is almost nothing). For now, I'm trying to work with Integers, only. I'll see for float numbers later!
With my code, I manage to display what I type (for instance, for "1 + 5" I get "1" then "5"), but when I press the "equal" button, I get a "NaN". I do know it stand for Not A Number, and I think I might be facing a problem of type.
So I tried in various parts of my code some "console.log(myVariables)" or make tests with "type of". I think I managed to find at which point I get a "NaN", but I cannot solve the problem.
Here's my code, so far, with comments so that you can follow my logic. :)
//here I target my buttons
numberOne.addEventListener('click', function() {screen.innerHTML += 1;});
numberTwo.addEventListener('click', function() {screen.innerHTML += 2;});
numberThree.addEventListener('click', function() {screen.innerHTML += 3;});
//... and so on for all different numbers and signs ...
//I initialize my variables
let firstNumber = "";
let secondNumber = "";
//I create an event for my "plus" and "equal" button
plusSign.addEventListener('click', add);
equalSign.addEventListener('click', equals);
//with this I'm clearing the output screen between two inputs ("firstNumber" and "secondNumber")
function clear() {
screen.innerHTML = "" ;
}
//I memorize the HTML value of the output screen in "firstNumber",
//I convert it into an integer,
//then I clear the output screen
function add() {
firstNumber = parseInt(screen.innerHTML);
clear();
}
//I memorize the current value on the screen and convert it into an integer too
function equals() {
secondNumber = parseInt(screen.innerHTML);
screen.innerHTML = firstNumber + secondNumber;
}
Now, "firstNumber" and "secondNumber" should be integers, shouldn't they?
I tried to "parseInt" in various parts of the code, but at the end it seems that the problem remains the same.
I know they are many code example on the internet for such a project, much more sophisticated, but I really was trying to think on my own. Maybe it is that I've been doing things the wrong way. Or maybe I just need to stop looking for the answer and retry tomorrow with a clear mind. ^^'