-
Notifications
You must be signed in to change notification settings - Fork 0
/
04_popShiftToRemoveItems.js
33 lines (25 loc) · 1.03 KB
/
04_popShiftToRemoveItems.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
/* Remove items from an Array with pop() and
shift() */
/* The pop() and shift() methods are near opposites
of push() and unshift() although neither takes an
argument */
let greetings = ['Whats up?', 'hello', 'see ya!'];
greetings.pop();
console.log(greetings); // ['Whats up?', 'hello']
greetings.shift();
console.log(greetings); // ['hello']
// Return the value of a removed element like this:
let popped = greetings.pop();
console.log(greetings); // []
/* We have defined a function, popShift, which takes an
array as an argument and returns a new array. Modify the
function, using pop() and shift(), to remove the first
and last elements of the argument array, and assign the
removed elements to their corresponding variables, so that
the returned array contains their values. */
function popShift(arr) {
let popped = arr.pop(); // Change this line
let shifted = arr.shift(); // change this line
return [shifted, popped]; // ['challenge', 'complete']
}
console.log(popShift(['challenge', 'is', 'not', 'complete']));