The browser storage localStorage is not available. Either your browser does not support it or you have disabled it or the maximum memory size is exceeded. Without localStorage your solutions will not be stored.
Array: shift() and push()
Like strings, arrays have several methods. Two of these methods are
The array method
The method
shift
and push
.
To remind you: Methods are functions that are applied to an object, here an array. Methods are appended with a point to the object.
The array method
shift
removes the first element from an array and returns that element.
The length of the array is reduced by 1.
let languages = ['C', 'Java', 'JavaScript'];
let firstLanguage = languages.shift();
The array languages
is created with 3 programming languages.
After shift
the array languages
contains only 2 items,
viz. ['Java', 'JavaScript']
. The value 'C'
is stored in firstLanguage
.
The method
push
adds a new element to an array.
The new element is passed as a parameter and is added to the end of the array.
let languages = [];
languages.push('C');
languages.push('Java');
languages.push('JavaScript');
Here an empty array is filled successively with the passed values.
Finally we get the array ['C', 'Java', 'JavaScript']
.Exercise
Write a function
Example:
rotate
that rotates the elements of an array.
All elements should be moved one position to the left.
The 0th element should be placed at the end of the array. The rotated array should be returned.Example:
rotate(['a', 'b', 'c'])
should return ['b', 'c', 'a']
.
+ Hint
Remove the 0th element with
shift
.
Add it again with push
.
+ Solution
function rotate(anArray) {
let element0 = anArray.shift();
anArray.push(element0);
return anArray;
}