I'm trying to calculate the user's input but when I'm trying to do a quick addition it outputs NaN= instead of the actual result
The user should be able to click the buttons to do the math, the problem is that my code can't get the user input before the user did the math
e.g. = User input: 1+4; My code can't get the 1 or the 4 and can't do math
Here is what I tried:
Code snippet :
var buttons = document.getElementsByClassName("buttons")[0];
buttons.onclick = (element) => {
if (element.target.className != "buttons") {
if (element.target.innerHTML == "=") {
for (var i = 0; i < result.innerHTML.length; i++) {
if (buttons.innerHTML = "+") {
result.innerHTML = element.target.value + i;
}
}
}
result.innerHTML += element.target.innerHTML;
console.log("User: " + element.target.innerHTML);
}
}
<div class="result">
</div>
<div class="buttons">
<div>←</div>
<div>C</div>
<div>%</div>
<div>/</div>
<div>7</div>
<div>8</div>
<div>9</div>
<div>x</div>
<div>4</div>
<div>5</div>
<div>6</div>
<div>-</div>
<div>1</div>
<div>2</div>
<div>3</div>
<div>+</div>
<div>0</div>
<div>.</div>
<div class="equal">=</div>
</div>
I can't edit the HTML and use jQuery.