Menu

How do I fix my calculators clear() function?

Reading 0:00
15s threshold

I'm trying to make a calculator. For as long as you are only doing one continuous equation there are no issues, but I can't use the clear button.

button:

<button onclick="clear()">C</button>

text field:

<input type="text" id="display" class="display">

calculator code:

<script>

//the working number

let numA = '';

//the previous working number to be operated on

let numB = '';

//the operation to be done between A and B

let oper = '';

function apndNum(number) {

numA += number;

document.getElementById('display').value = \ ${numB} ${oper} ${numA}`;`

}

function processOper(operation) {

if (numA === '') return;

if (numB !== '') {

calculate();

}

oper = operation;

numB = numA;

numA = '';

document.getElementById('display').value = \ ${numB} ${oper}`;`

}

function calculate() {

if (numB === '' || numA === '') return;

//better naming conventions might be needed here since the names get flipped

let output;

let A = parseFloat(numB);

let B = parseFloat(numA);

switch (oper) {

case…

Continue reading — create a free account

Join HashtagPLUS to read full articles, follow hashtags, vote, and join the conversation.