1

I'm making a simple calculator and my function is not working:

HTML:

<div id="mathLine">0</div>
<div id="score">none</div>
<button id="clear" class = "num" ocnlick="clear(0)">C</button>
<button id="exp" class = "num" >exp</button>
<button id="sqrt" class = "num">sqrt</button>
<button id="buttonide" class = "num">/</button>
<button id="seven7" class = "num" onclick="numerical(7)">7</button>
<button id="eight8" class = "num" onclick="numerical(8)">8</button>
<button id="nine9" class = "num" onclick="numerical(9)">9</button>
<button id="multiply" class = "num" onclick="numerical('X')">X</button>

JS

var equation = [];  
function numerical(number){
    equation.push(number);
    document.getElementById('mathLine').innerHTML = equation;
    console.log(equation);
};
function clear (j) {
    for (j; j < (equation.length+2); j++){
    equation.pop();
    }
    console.log(equation);
    document.getElementById('mathLine').innerHTML = "0";
};

Some more info:

  • js file is correctly implemented in html;
  • the first function (numerical) is working fine;
  • List item when I tried it in separate html file, where I put html and js in tag it all worked

So I have no idea, what's wrong... Maybe I'm looking at it too long and I'm not seeing something obvious?

6
  • Have you tried using a debugger such as FireBug to get any error messages? Commented Aug 3, 2016 at 19:36
  • 1
    You haven't asked a question, and you haven't said specifically what isn't working. What exactly is the problem you need help with? Commented Aug 3, 2016 at 19:36
  • @dlsso It's not as clear as it could be, but Olka says that the first function works, but not the second - and the question title indicates .innerHTML is not working as expected. It sounds like the mathLine's text does not change. Commented Aug 3, 2016 at 19:38
  • StackOverflow is not your personal debugger to detect your typos. Use a proper debugger. Even the StackOverflow syntax highlighter tells there is something wrong with that event handler. Commented Aug 3, 2016 at 19:41
  • #atjoedonahue - no, I will try this one. Thanks. Commented Aug 3, 2016 at 19:59

2 Answers 2

1

You have a typo. Try changing "ocnlick" to "onclick."

Sign up to request clarification or add additional context in comments.

1 Comment

Oh, that's right! It's still not working, but it's one step forward:)
0

My belief here is that your array-clearing is not working - it's usually not safe to iterate through an array while you are removing elements from it, as the indexing can get messed up. You could perhaps replace the for loop with a while loop checking the array's length, but a simple suggestion would be instead to reassign the array:

equation = [];

1 Comment

I'll try this. Thank you!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.