0

I am trying to add a value to a variable using the += function. This is the code I am using:

function getAnswer() {
    var num1 = Number(document.getElementById('numone').value);
    var num2 = Number(document.getElementById('numtwo').value);
    var oper = document.getElementById('oper').value;
    var numberOfEquation = 0;
    numberOfEquation += 1;
    if (oper == '+') {
        var p = document.createElement('p');
        var txt = document.createTextNode(num1+num2 + ' - Equation ' + numberOfEquation);
        p.appendChild(txt);
        document.body.appendChild(p);
    } else if (oper == '-') {
        var p2 = document.createElement('p');
        var txt2 = document.createTextNode(num1-num2 + ' - Equation ' + numberOfEquation);
        p2.appendChild(txt2);
        document.body.appendChild(p2);
    }
    console.log('You did an equation!');
}

I don't know what went wrong.

Barmar
  • 741,623
  • 53
  • 500
  • 612
L. O. L.
  • 1
  • 3
  • 2
    Yes, and? What went right? What's the problem here? You can do `v++` or `++v` or `v += 1`, all of which will increment a variable. Are you looking to increment this each time you run the function? If so you can't use a local variable, you must declare `numberOfEquation` outside of the function. – tadman Apr 16 '19 at 18:03
  • 1
    You are declaring the variable then adding 1 to it, it will always be 1 here. – hawkstrider Apr 16 '19 at 18:03
  • Just because you're not using spaces doesn't mean `num1+num2` is passed any differently than `num2 + ' - Equation '` - wrap your equation in parenthesises. – h2ooooooo Apr 16 '19 at 18:03
  • 3
    I guess you want to move `var numberOfEquation = 0;` out of the function – Jonas Wilms Apr 16 '19 at 18:04
  • Just poor hoisting. Move the var out. Like apparently Jonas states seconds before my comment :D – Chris W. Apr 16 '19 at 18:04
  • 1
    @ChrisW. This has nothing to do with hoisting. It's just variable scope -- he's creating a new variable each time the function is called. – Barmar Apr 16 '19 at 18:05
  • @Barmar right, how's that not hoisting? If the var sits outside of the method... Or is just verbiage semantics? – Chris W. Apr 16 '19 at 18:06
  • @chrisW hoisting would be `let a = 1; { console.log(a); let a = 2; }` – Jonas Wilms Apr 16 '19 at 18:09
  • Ya you're right I'm using the verbiage incorrectly. Not a term I use often obviously, cheers. – Chris W. Apr 16 '19 at 18:13

1 Answers1

2

It appears to be a misunderstanding of how local variables work.

Local variable:

 function x() {
   var y = 0;
   ++y;
   return y;
 }

 x(); // => 1
 x(); // => 1
 x(); // => 1

This returns 1 each time since var y explicitly declares a local variable. It will only exist during the execution of that function. As soon as the function terminates that variable ceases to exist. When the function starts up again it makes a brand new one.

Here's a different approach with a persistent variable:

 var y = 0;

 function x() {
   ++y;
   return y;
 }

 x(); // => 1
 x(); // => 2
 x(); // => 3

This is because y exists outside the scope of the function. It lives as long as your program does.

tadman
  • 208,517
  • 23
  • 234
  • 262