'Javascript - if-statement with multiple numeric conditions doesn't work

I'm trying to make a simple program in javascript+html. If exp exceeds within a certain range/exceeds a certain number, the level displayed goes up by 1. I've tried to make it show onload, but the level doesn't change no matter what happens to the exp staying at the highest one I've written code for so far.

Javascript:

var exp6 = localStorage.exp6;
var pexp6 = parseInt(exp6);

function char6() {
    res.innerHTML = res6;
    var lsps = pexp6;
    localStorage.setItem("lsp", lsps);
    return PrUpdate();
}

var lsp = localStorage.lps;

function PrUpdate() {
    if (lsp <= 999) {
        lvl.innerHTML = 1;
    }
    else if (lsp >= 1000 && lsp <= 1999) {
        lvl.innerHTML = 2;
    }
    else if (lsp >= 2000 && lsp <= 2999) {
        lvl.innerHTML = 3;
    }
    else if (lsp >= 3000 && lsp <= 3999) {
        lvl.innerHTML = 4;
    }
    else if (lsp >= 4000 && lsp <= 4999) {
        lvl.innerHTML = 5;
    }
}

I've also included the setChar() function in the window.onload of the page. I've tried including the function itself in it as well, but whether I add it at the end of the setChar() or in the window.onload the problem stays the same. All other functions work fine, it's just this part that doesn't. I'm also trying to make it generic to fit other 'accounts' I'm trying to make to save myself time. But I just can't make it work.

Edit:

I've figured out why it didn't work, and it was cause I had my localStorage.lsp in the wrong place.

I'm trying to figure out now how to make it so I don't have to refresh the page to get it to appear.

[ Solved my issue :), if unclear by my edit above]



Solution 1:[1]

The way you are trying to access values from localstorage is incorrect. This is not valid: localstorage.exp6.

Instead try this: localstorage.getItem('exp6')


See the documentation of Web Storage API for more information

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 paul