Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7715669
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T02:26:41+00:00 2026-06-01T02:26:41+00:00

I have the following used to create a dummy high score table if if

  • 0

I have the following used to create a dummy high score table if if localscores doesn’t exist in local storage:

if (localStorage.getItem("localScores") === null) {

highscores = [
  {user: "Player 1", score: 100},
  {user: "Player 2", score: 90},
  {user: "Player 3", score: 80},
  {user: "Player 4", score: 70},
  {user: "Player 5", score: 60},
  {user: "Player 6", score: 50},
  {user: "Player 7", score: 40},
  {user: "Player 8", score: 30},
  {user: "Player 9", score: 20},
  {user: "Player 10", score: 10}
  ];
  localStorage['localScores']=JSON.stringify(highscores);
  console.log("doesn't exist")
}

else{
highscores = JSON.parse(localStorage['localScores']);
console.log("exists")
}

I always want to maintain 10 scores so I have this to add a score, where username and total are the name entered and score achieved:

highscores.push({user: username, score: total});                               
highscores.sort(function(a,b){ return b.score - a.score});    
delete highscores[10];
localStorage['localScores']=JSON.stringify(highscores);
console.log(highscores);

Everything is fine until I reload the game and the first function runs, it seems to add null value to the highscores array. So if I add 5 additional scores I will have 3 null values in the array?

[Object, Object, Object, Object, Object, Object, Object, Object, Object, Object, null, null, null, null, null]

This leads me to believe either the line where I evaluate if the localscores exists is wrong
(localStorage.getItem(“localScores”) === null)

Or where I delete the 11th element in the array is wrong
delete highscores[10];

Or there’s some other issue I don’t see, if anyone knows what’s going on I’d very much appreciate some help.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-01T02:26:44+00:00Added an answer on June 1, 2026 at 2:26 am

    When you call delete on an Array instance, you don’t actually make the array shorter, you just replace the value at the specified index with undefined.

    If the length of highscores should always be no greater than 10, and the item to be removed will always be the last, you could simply use highscores.length = 10.

    You could also use highscores = highscores.slice(0,10);, but as jfriend00 pointed out, it does involve the extra overhead of creating a new array.

    Although, I feel compelled to mention that the performance hit of creating a new array is really just a drop in the bucket compared to the amount of work the browser will be doing to encode/decode JSON and read/write from localStorage, which is notoriously slow.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have used following code to create a simple PDF file. It executes fine
i have used the following headers in order to create xls files that contains
I have the following tables (removed columns that aren't used for my examples): CREATE
I have the following script to create a table in MySQL version 5.1 which
I have used the following steps to create a login page. 1> create the
i have used the following xml code to create the layout. <?xml version=1.0 encoding=utf-8?>
I have used svn_load_dirs.pl to create vendor branches and I have the following versions
We have the following table: CREATE TABLE [dbo].[CampaignCustomer]( [ID] [int] IDENTITY(1,1) NOT NULL, [CampaignID]
I want to create VIEW definitions on MS Access. I have used following CREATE
I have used following 2 patterns to create a view. @property (retain, nonatomic) SomeView*

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.