Assignment GDB Solution: Javascript

loading...

Javascript unshift array method

unshift Adds one or more elements to the beginning of an array and returns the new length of the array. Syntax arrayName.unshift(element1, …, elementN) Example // assumes a println function exists myFish = [“angel”, “clown”]; println(“myFish before: ” + myFish); unshifted = myFish.unshift(“drum”, “lion”); println(“myFish after: ” + myFish); println(“New length: ” + unshifted); Output […]

Javascript Shift array method

Shift Removes the first element from an array and returns that element. This method changes the length of the array. Syntax array.shift() Example // assumes a println function is defined var myFish = [“angel”, “clown”, “mandarin”, “surgeon”]; println(“myFish before: ” + myFish); var shifted = myFish.shift(); println(“myFish after: ” + myFish); println(“Removed this element: ” […]

Javascript Splice array method

Splice Changes the content of an array, adding new elements while removing old elements. Syntax array.splice(index, howMany[, element1[, …[, elementN]]]) Example // assumes a print function is defined var myFish = [“angel”, “clown”, “mandarin”, “surgeon”]; print(“myFish: ” + myFish); var removed = myFish.splice(2, 0, “drum”); print(“After adding 1: ” + myFish); print(“removed is: ” + […]