Reputation: 13
Every time you load the page or click the #desc div, the content should change to a different array item. There will be more, that's why there's Math.random().
Problem is, the loop that should generate a different number every time isn't working.
EDIT: Thanks for all responses, changing === to == helped ;)
var descs = [
"^that's me",
"make the web great again"
];
function changeDesc() {
var randNum = Math.abs(Math.round(Math.random()*descs.length-1));
if (randNum == localStorage.getItem("usedDescs")) {
changeDesc();
}
else {
localStorage.setItem("usedDescs", randNum);
$("#desc").html(descs[localStorage.getItem("usedDescs")]);
}
}
$(document).ready(function(){
changeDesc();
$("#desc").click(function() {
changeDesc();
});
});
Upvotes: 1
Views: 46
Reputation: 9358
Your problem is that you compare the integer randNum
with the string localStorage.usedDesc
Do this and see for yourself:
console.log(typeof localStorage.usedDescs)
console.log(typeof randNum)
Using localStorage.set/getItem()
is the 'correct' way to do it but what you have written is valid as well.
Upvotes: 1
Reputation: 11339
Localstorage values are always strings, no matter the input. So the comparison between randNum and localStorage.usedDescs will always fail.
Upvotes: 0