1

Code: http://pastebin.com/xGa9VLDY

The question:
I am making a little calculator app to hone my JavaScript skills, and it turned out alright. However, the problem I am experiencing is that JavaScript returns my variable 'memory' as 0, even though the value is increased (or decreased), and I can't seem to figure out how, so the calculator is pretty much useless, since the equal button only returns 0. I've tried to use the console in Chrome to increase the value, just to test and this is my result:

memory + 5
5

But when I try to check the value of 'memory' again:

memory
0

Is it something I am missing, or is it just a stupid, little mistake?

What I've tried:
As you can see, I have now tried to store the value in localStorage, but to no avail, and I do not see what else I can do. I recently switched from

memory += textBox.value;

to

memory = memory + textBox.value;

but obviously, that didn't work either.

EDIT:

I have got a very strange problem now:

memory: 0
textBox.value: "6"
parseInt(textBox.value): 6
memory + parseInt(textBox.value): 6
typeof(memory): "number"

This is all the values when the textbox still is populated with a number, and this is these are values right after pressing the plus sign:

memory: NaN
textBox.value: ""
parseInt(textBox.value): NaN
memory + parseInt(textBox.value): NaN
typeof(memory): "number"
gloriousCatnip
  • 411
  • 1
  • 6
  • 18
  • Calling `memory + 5` will not change the value stored in your `memory` variable, it'll simply return the result of adding `5` to its value. To update its value you'd have to use `memory += 5`. – James Donnelly Mar 16 '15 at 10:16

2 Answers2

6

The console is giving you the result of your mathematical operation, not storing it anywhere.

So you start with memory containing the value 0. To make that 5, you need to add 5, and then store the result back in memory.

memory = memory + 5;

There is a shorthand for that:

memory += 5;

And also a shorthand for just adding 1 (because it's a very common task):

++memory;

although this is not considered best practise.

Regarding the textBox.value version, where you are doing this, the value property of a textBox is a string, not a numeric value. Even though 1234 looks like a number, it is actually the characters '1', '2', '3' and '4'. You could happily include 'a' or 'z'. To get a numeric value out that you can use in a mathematical expression, you need to parse the string:

Either

enteredValueInt = parseInt(textBox.value); // or
enteredValueFloat = parseFloat(textBox.value);

depending on whether you expect the textBox to have a whole number or a floating point value in.

Demo here: http://jsbin.com/yijezifowu/1/edit?html,js,output

EDIT: Re 'very strange problem':

You have a spurious call to emptyBox at the top of the mathStuff function in your pastebin code:

function mathStuff(operator) {
    emptyBox();
    //... stuff with operators
}

So you're beginning by clearing the textbox before attempting to retrieve the numbers.

Community
  • 1
  • 1
Phil H
  • 19,928
  • 7
  • 68
  • 105
  • 1
    @Oleg: Without wanting to get into a debate here,`++var` is a better practise in general, and for beginners in particular. Teaching them `var++`, although easier to remember, is far more likely to cause them unexpected bugs when they do `callFunc(var++)` or to produce bad coding practise later. Post-increment is almost always a bad plan. – Phil H Mar 16 '15 at 10:23
  • Never wanted to get into a debate, but both `i++` and `++i` are part of the language (and both are frowned upon by JSLint by the way). – Oleg Mar 16 '15 at 10:25
  • 1
    Ok, I'll edit the answer, but I'm not promoting post-increment! – Phil H Mar 16 '15 at 10:32
  • Thank you for a swift answer! I tried to parse the number in the text box and adding it to the 'memory' variable, but when I check the console or press equals, the calculator returns 'NaN' - not a number. This is what I try: `memory += parseInt(textBox.value);` – gloriousCatnip Mar 16 '15 at 10:34
  • @gloriousCatnip: can you give us the value in your textBox, then? i.e. `console.log(textBox.value)`? – Phil H Mar 16 '15 at 10:36
  • Well, first of all the textbox obviously starts off empty. If I add some numbers and try to use the console to `console.log` it, the log will be the number. If I try to use `typeof(textBox.value)`, I get string. But I parse the value to an int, so I don't really see the problem. :/ – gloriousCatnip Mar 16 '15 at 10:39
  • @gloriousCatnip: I've created a jsbin and added a link. If you're not getting a value properly, then something else must be happening either to your value. – Phil H Mar 16 '15 at 10:52
0
memory + 5

is a calculation which yields the sum of the two values, which is 0 in your case, since memory is 0. You want to store the value in memory, but you need to assign the value to your memory variable, like this:

memory = memory + 5;

or, a shorthand version:

memory += 5;

Read more about assignment operators here.

Also, you must make sure the operator you are using will work well, as:

'0' + 5 === '05'

while

0 + 5 === 5
Lajos Arpad
  • 64,414
  • 37
  • 100
  • 175