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.

aswathy
  • 821
  • 3
  • 15
  • 27
  • 3
    Reading & writing local files can be problematic because of browser security restrictions. It sounds like what you need is Javascript's [Local Storage](http://diveintohtml5.info/storage.html) –  Mar 29 '14 at 07:41
  • @Pilot Not quite what I wanted...Didn't understand much – aswathy Mar 29 '14 at 07:41
  • @MikeW You might want to post this as an answer. It's probably the best the OP is going to get. – mjhm Mar 29 '14 at 20:46
  • Local storage works fine for me. The problem is solved. Thanks. – aswathy Mar 30 '14 at 05:43

1 Answers1

-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.

Samuel
  • 15
  • 1
  • 4