Impact

This forum is read only and just serves as an archive. If you have any questions, please post them on github.com/phoboslab/impact

1 decade ago by peepli

Hello,

So I am trying to increment my localStorage value everytime a condition is true using the plugin provided by jsantell. But it doesn't seem to work

Below is my block of code where I try to implement this

for(var j = 0; j < inputArray.length; j++){
for(var k = 0; k < this.probsSolved.length; k++){
if( inputArray[j] == this.probsSolved[k] ){

this.storage.set(probSolKey, '' +parseInt(this.storage.get(probSolKey))+1);

console.log(this.storage.get(probSolKey));

if(this.storage.get(probSolKey) == "NaN".concat(totalProbToSolve)){
this.storage.set(achkey,1);
this.triggerAchievements(id,name);
}
}
}
}

So as localStorage only accepts strings, someone on StackOverflow suggested me to do parseInt and increment the value by 1 everytime the condition is met. So I want to increment my localStorageValue of probSolKey every time my if condition after inner loop executes to true.

So for the first time I get the value as NaN1 on my console.log. It's a weird value but for now I am just fine with it.
Next time again when my condition goes true my localStorage value stays at NaN1 and does not increment to NaN2.

I am sure my condition meets and I can reach the console.log everytime the condition is true but the value of probSolKey does not increment.

What could be the problem??

1 decade ago by mglnunez

Basically NaN stands for Not a Number. So you are putting something that is not a number and trying to parse it as an integer, so that "NaN1" value will parse back to "NaN".

To get integers back use the getInt function. Make sure you are putting numbers in the local storage to begin with.
Page 1 of 1
« first « previous next › last »