I am working on personal project and need help bout localstorage. Everything works great except what I’m trying to do is when get data (score) in localstorage being updated (new score is added to current highscore already in localstorage.
So far, what does it do is replacing value in localstorage with new (for example, if current highscore is 200 and user has score of 50, I want the new highscore be 250. If someone can help me. Thanks in advance
Firstly, welcome to the forums.
While we are primarily here to help people with their Free Code Camp progress, we are open to people on other paths, too. Some of what you are asking is pretty trivial in the Free Code Camp context, so you might find that if you’re not getting the instruction and material you need in your current studies, the FCC curriculum will really help you get started. At a modest guess I’d say investing a 4-5 hours working through the curriculum here will really pay off. You can find the curriculum at https://www.freecodecamp.org/learn.
With your current questions, we don’t have enough context to know what you already know or don’t know, so it is impossible to guide you without just telling you the answer (which we won’t do).
It is pretty typical on here for people to share a codepen / repl.it / jsfiddle example of what they have tried so that anyone helping has more of an idea of what help is actually helpful.
Please provide some example of what you’ve tried and I’m sure you’ll get more help.
Happy coding
Hi everyone,
I’m working on a project (quiz), and what I’m trying to do is after the user’s score has been placed in localStorage, if the user plays again, that its score is added and keep increasing the highscore in localStorage.
I’ve tried many many things without success. Thanks for your input
This might be of some help.
You’ll need to share your code with us for more detailed assistance.
Have you tried the addition assignment, maybe? Read about it here:
Have you tried something like, “when the user clicks the button, the score in localStorage = the score in localStorage + the additional score?”
Which, in shorthand, is “score in localStorage += additional score”.
If you’re having issues transferring the score back and forth from localStorage, you could study how that works a bit more, as @robheyays kindly provided the resource to.
Hope you solve the problem soon!
some things I’ve tried without knowing if it’s doable or not:
(just part of my code):
score = 0
startGame = () => {
questionCounter = 0
availableQuestions = [...questions]
document.getElementById("highScore").innerHTML = localStorage.getItem("score")
getNextQuestion();
}
getNextQuestion = () => {
if(availableQuestions.length === 0 || questionCounter > MAX_QUESTIONS) {
let highScore = highScoreText.innerText
highScore = +highScore + +score
localStorage.setItem("highScore", highScore)
return alert ("Game Over")
}
All I want to do is add current score to highscore. So far, all I did is replacing highscore in storage by a new one.
When trying to add them together into a new variable, it simply doesnt work
e.g. currentHighScore = highscore + score, it doesnt work when returning variable highscore. But if declare this way (of course knowing the value right at the beginning, it works - highscore = 10 / score = 5 / currentHighScore = highscore + score (result 15).
I even thought about highscore = document.getElementById("highscore) + score and
localStorage.setItem(“highscore”, highscore) + score
localStorage.setItem(“highscore”, highscore + score)
localStorage.getItem(“highscore”) + score
I’ve also tried highscore = highScoreText.innerText + score
and:
let score = 0
let highscore = 0
let currentHighScore = 0
then:
localStorage.setItem(“highscore”, highscore)
…
document.getElementById(“highscore”) = highscore
currentHighScore = highscore + score
localStorage.setItem(“highscore”, currentHighScore)
hope this make more sense on what I’m trying to achieve
P.S. I only have some basic of JS, so it’s really possible things I tried to do don’t make any sense at all LOL
So, I’m really not able to understand exactly what’s going on there, but here are some clues I can give:
- Make sure you’re converting data types properly. I think you’re trying to do math on a number and a number in a string (which is a string). For example,
let highScore = highScoreText.innerText
highScore = +highScore + +score
here “highScore.innerText” is a string. You’re trying to add it “score” or something, but that’s like adding “6” + “6” — you’ll get a string looking like “66” instead of a number, 12. You can convert strings to integers by using the Number()
function, i. e. Number(highScore.innerText)
.
Basically, make sure you’re doing math with integers, and not strings.
- Next, I see some strange math going on.
highScore = +highScore + +score
I don’t know about you, but that seems like too many +
signs to me. You don’t need to specify variables to be positive in JS.Maybe you tried to do something else there, I don’t know.
-
When debugging, and in general just coding up your ideas, use the
console.log()
statement a ton. I mean, a ton. When something doesn’t work, start logging suspicious variables, and carefully try to draw conclusions based on what you see. For example, ifconsole.log(a)
shows a number andconsole.log(b)
also shows a number, butconsole.log(c)
doesn’t work, andc
is supposed to bea + b
, that means something might be wrong with your addition part.
So, useconsole.log()
to eliminate possible issues and narrow down the error scope. -
I can see by what you’re saying, that you’re really trying a ton of different solutions. I get this way when I’m tired, just tweaking things mindlessly and getting frustrated when nothing I do works, so I understand you. But what you should do is take a deep breath, focus, and try to walk through each step of your code. Read though the lines out loud, saying what each line should do. See if you notice any logical contradictions. If not, what parts are you suspicious of? Then carefully check them, log them to the console, document and verify each step of your code.
You code has to make sense to you, every line of it, otherwise it won’t work.
Thanks for the advice, Nick. Yes, many things I’ve tried and that I’ve included in my code above is after I read, google, and watch different videos (the + + sign as a matter of fact) lol. I wasn’t sure that innerText would have been considered a string so I will keep this in mind. Thanks again. You’re the most helpful source I’ve seen so far
You’re welcome! Hope you solve it soon!
Keep in mind that this is the increment operator, which increases by 1. It goes in front of a variable like this:
let x = 5;
x++
// x now equals 6
Good luck!