Skip to content

Instantly share code, notes, and snippets.

@garywoodfine
Last active October 21, 2017 15:32
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 garywoodfine/c6f3160b860b96399db354d37265bd42 to your computer and use it in GitHub Desktop.
Save garywoodfine/c6f3160b860b96399db354d37265bd42 to your computer and use it in GitHub Desktop.
// Code for the blog post https://garywoodfine.com/arrays/
// Decalre the array
var fibonacci = new Array(); // By the end of this our array will contain 1,1,2,3,5,8,13,21,34,55,89
// or
var fibonacci = [];
fibonacci.push(1);
fibonacci.push(1);
fibonacci.push(2);
fibonacci.push(3);
fibonacci.push(5);
console.log("The fibonacci array will contain " + + fibonacci.push(8) + " if we add 8 " );
// If we print the array to the console now,
console.log(fibonacci);
// Pop the last element of the array. This
console.log("The Last element of fibonacci : " + fibonacci.pop());
// Check the length and notice 8 has is not there ?
console.log("There are " + fibonacci.length + " in the fibonacci ");
// Join 2 arrays together using the concat method
console.log( fibonacci.concat([8,13,21,34,55,89]));
console.log("Print in reverse order array");
console.log(fibonacci.reverse());
console.log("Splice the Array")
//Splice the array
// We will use the splice method to insert missing numbers within the fibonacci sequence
fibonacci = [ 1,1,13]; //Incorrectly defined the array
console.log(fibonacci);
fibonacci.splice(2,0, 2,3,5,8) // Insert
console.log(fibonacci);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment