2

I am trying to make a save function for my game and, it isn't allowing me to save any of my Variables as integers (even with parseInt(Variable), +Variable, etc.)

The answers to this post aren't working.

    localStorage.value = value;
    value = localStorage.value;
qwerty77asdf
  • 93
  • 3
  • 11

3 Answers3

4

Try to use parseInt(localstorage.numericProperty)

function populateStorage() {
  let obj = {x: "sdfsd", y: "sdfsdf"};
  
  localStorage.setItem("image", JSON.stringify(obj));

  localStorage.bgcolor = "blue";
  
  localStorage.numeric = 3;
}

function checkStorage(){
  console.log(localStorage.getItem("image"));
  console.log(localStorage.bgcolor);
  console.log(parseInt(localStorage.numeric) + 1);
}

populateStorage();
checkStorage()
Tiago Fabre
  • 739
  • 5
  • 18
  • look my example with objects – Tiago Fabre May 10 '16 at 11:50
  • try to run your code in codepen, since I tested that and it is working fine. – Tiago Fabre May 10 '16 at 12:01
  • 1
    when you load from local storage you need to use parseint(localstorage.value) as in the example that I did in the learn css http://codepen.io/tiagofabre/pen/BKbyGq ` learnSpeed = parseInt(localStorage.learnSpeed); ` – Tiago Fabre May 10 '16 at 12:19
  • 1
    look the example in code pen. add larncss save, load and try to learn more css... – Tiago Fabre May 10 '16 at 12:29
  • 1
    I think you are not parsing every variables and it is causing a sum between an integer and a string... – Tiago Fabre May 10 '16 at 12:44
  • 1
    Since in the codepen is working it could be something from your environment (os, browser or something like that) – Tiago Fabre May 11 '16 at 01:00
  • 1
    did you see this working in the codepen ? I put an example there and worked for me... http://codepen.io/tiagofabre/pen/BKbyGq test if is working: Click in leanrCSS -> Save -> Load and learnCSS again – Tiago Fabre May 11 '16 at 01:53
2

The best way to convert any number data in localStorage from string to number:

var a = localStorage['some_property'];

typeof a; // "string"

var b = +localStorage['some_property'];

typeof b; // "number"
Ali Mamedov
  • 5,116
  • 3
  • 33
  • 47
1

u can an object for storing in local storage as follows

  Storage.prototype.setObject = function(key, value) {
    this.setItem(key, JSON.stringify(value));
}

Storage.prototype.getObject = function(key) {
    var value = this.getItem(key);
    return value && JSON.parse(value);
};


    localStorage.setObject('x', {1: 2, 2: "s"})
    console.log(localStorage.getObject('x'));
  • That includes JSON & jQuery, any other ways? – qwerty77asdf May 10 '16 at 11:33
  • 1
    This is: `$(function() { ... });` – Ali Mamedov May 10 '16 at 11:36
  • @AliMamedov is that not jQuery? – qwerty77asdf May 10 '16 at 11:37
  • :) ... okay remove $. – 92sharmasaurabh May 10 '16 at 11:38
  • 1
    @qwerty77asdf, Yes, you are completely right. It is jQuery! – Ali Mamedov May 10 '16 at 11:39
  • use like this Storage.prototype.setObject = function(key, value) { this.setItem(key, JSON.stringify(value)); } Storage.prototype.getObject = function(key) { var value = this.getItem(key); return value && JSON.parse(value); }; localStorage.setObject('x', {1: 2, 2: "s"}) console.log(localStorage.getObject('x')); – 92sharmasaurabh May 10 '16 at 11:39
  • @92sharmasaurabh modifying built-in objects is not a good idea... `Storage.prototype.setObject = function(key, value) {...}` – Ali Mamedov May 10 '16 at 11:43
  • 1
    Some words about Augmenting built-in objects. **Object-Oriented JavaScript - Second Edition:** Augmenting built-in objects through the prototype is a powerful technique, and you can use it to shape JavaScript in any way you like. Because of its power, though, you should always thoroughly consider your options before using this approach. The reason is that once you know JavaScript, you're expecting it to work the same way, no matter which third-party library or widget you're using. **Modifying core objects could confuse the users and maintainers of your code and create unexpected errors** – Ali Mamedov May 10 '16 at 11:45