Skip to content

Instantly share code, notes, and snippets.

@mwanjajoel
Last active May 19, 2020 15:58
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mwanjajoel/6195d8cd09e958a4412b5c7e2f5a5138 to your computer and use it in GitHub Desktop.
Save mwanjajoel/6195d8cd09e958a4412b5c7e2f5a5138 to your computer and use it in GitHub Desktop.

The array on which we are going to perform splice methods.

const weekDays = ["Sunday", "Tuesday", "Monday", "Wednesday"];

Objective is to remove Tuesday from the current position and then add it back in the right position. Go to this link https://www.w3schools.com/jsref/jsref_splice.asp for more details

Step 1: Assign Tuesday to a variable

var day = "Tuesday"

Step 2: this code means, at position 1, remove 1 item and store the result.Tuesday happens to be in that position.

weekDays.splice(1, 1)

Step 3: now time to add it back but in a different position using the day variable. This code means, at position 2, remove 0 items and add 1 item called day

weekDays.splice(2, 0, day)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment