Variable produces 1111 instead of 4

real sorry if the title sucks.

So my deal here is that I’m making a cookie clicker wannabe (just to see if i can tbh) and I’m having an issue with freaking counting.

So I have code that is like this.
var licks:Number = licks + 1;

And instead of making a number like 4 it makes 1111. Please help with this if you can find an answer, I don’t actually know how to program.

This usually means you’ve got a string mixed up in there somewhere. The reason this happens is because both strings and numbers use the + operator. Numbers use it to add numbers together whereas strings use it for concatenation.

1 + 2 = 3 // numbers
"hell" + "o" = "hello" // strings

When you use + with strings that are themselves numbers or a combination of strings and numbers, the behavior of + will be to concatenate as strings

"1" + "1" = "11" // strings as numbers
"1" + 1 = "11" // strings and numbers

So if you’re using + and getting “1111” instead of 4, then its most likely because you have a string in there somewhere. And if you’re not sure, you can force your values to be numbers by using Number() to convert them

var licks = "1"
licks + 1 = "11" // no conversion
Number(licks) + 1 = 2 // with conversion
1 Like

thank you so much

ALSO i will be puiblishing this project so would you like a credit?

1 Like

Welcome! And no credit is necessary :wink:

1 Like