1

i am an amateur programmer and I'm trying to make a javascript game. I am not going to post this game to the internet, so don't bother with servers and stuff.

The game has a simple score keeping system and every time the game is over, I want the all-time-high score to be shown, and if a new highscore is obtained, I want the previous highscore to be overwritten with the new one.

I searched a lot but couldn't find a proper read/write in javascript tutorial anywhere.

Also, if it can be written to a file that cannot be edited by anyone like txt files, it would be better.

I use plain javascript only, no Jquery or JSON or anything like that.

4
  • 3
    Reading & writing local files can be problematic because of browser security restrictions. It sounds like what you need is Javascript's Local Storage Commented Mar 29, 2014 at 7:41
  • @Pilot Not quite what I wanted...Didn't understand much Commented Mar 29, 2014 at 7:41
  • @MikeW You might want to post this as an answer. It's probably the best the OP is going to get. Commented Mar 29, 2014 at 20:46
  • Local storage works fine for me. The problem is solved. Thanks. Commented Mar 30, 2014 at 5:43

1 Answer 1

-1

Since it is just a plain javascript file, you will have to keep a temporary score.

You can do something like this

var score = userScore; // Put whatever the user scores as this variable
var highScore = 0;
if(score > highScore) {
highScore = score;
console.log("High Score: " + highScore);
}

I tried to make this as simple as possible since it seems you are just starting out in javascript. What this if statement above does is checks if the score is greater than the current high score and will replace the old high score with the new one.

Sign up to request clarification or add additional context in comments.

1 Comment

I used local storage to solve the problem.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.