As a newcomer to JavaScript, I've been working on creating a basic calculator to practice my skills. However, I've run into an issue. When entering a double-digit number, the calculator is adding the digits together instead of treating them as separate numbers. For example, inputting 12 + 5 results in the answer 8. I'm struggling to resolve this problem and would appreciate any suggestions or guidance. Thanks for your help! PS: Sorry for the lengthy explanation. `
</div>
<button type="button" onclick="putToScreen(1); xValue += parseInt(1)" class="inputButton1 input" value="1">1</button>
<button type="button" onclick="putToScreen(2); xValue += parseInt(2)" class="inputButton1 input" value="2">2</button>
<button type="button" onclick="putToScreen(3); xValue += parseInt(3)" class="inputButton1 input" value="3">3</button>
<button type="button" onclick="putToScreen('+'); plusButton++" class="inputButton1" value="+">+</button>
<button type="button" onclick="putToScreen(4); xValue += parseInt(4)" class="inputButton2 input" value="4">4</button>
<button type="button" onclick="putToScreen(5); xValue += parseInt(5)" class="inputButton2 input" value="5">5</button>
<button type="button" onclick="putToScreen(6); xValue += parseInt(6)" class="inputButton2 input" value="6">6</button>
<button type="button" onclick="putToScreen('-')" class="inputButton2" value="-">-</button>
<button type="button" onclick="putToScreen(7); xValue += parseInt(7)" class="inputButton3 input" value="7">7</button>
<button type="button" onclick="putToScreen(8); xValue += parseInt(8)" class="inputButton3 input" value="8">8</button>
<button type="button" onclick="putToScreen(9); xValue += parseInt(9)" class="inputButton3 input" value="9">9</button>
<button type="button" onclick="putToScreen('*')" class="inputButton3" value="*">*</button>
<button type="button" onclick="clearScreen()" class="inputButton" value="C">C</button>
<button type="button" onclick="putToScreen(0); xValue += parseInt(0)" class="inputButton4 input" value="0">0</button>
<button type="button" class="inputButton4" onclick="compute()" value="=">=</button>
<button type="button" onclick="putToScreen('/')" class="inputButton4" value="/">/</button>
</div>
</div>
<script type="text/javascript">
var input = document.getElementsByClassName("inputButton");
//var valueBox = document.getElementById("answer");
var answer = document.getElementById("answer");
// var xValue = do i +=
var xValue = 0;
var yValue = 0;
var plusButton = 0;
//var finalAnswer
function putToScreen (x) {
var node = document.createTextNode(x);
answer.appendChild(node);
}
function clearScreen() {
answer.innerHTML = "";
xValue = 0;
yValue = 0;
plusButton = 0;
}
function add () {
if (plusButton >= 1) {
document.getElementsByClassName("input").onclick = "yValue +=";
}
var sum = parseInt(xValue) + parseInt(yValue);
answer.innerHTML = sum;
}
//var sum = parseInt(xValue) + parseInt(yValue);
function compute () {
if(plusButton >= 1) {
add();
}
}
`