* chore(learn): audit basic algorithm scripting * chore(learn): audit basic data structures * chore(learn): audit basic javascript * chore(learn): audit debugging * chore(learn): audit es6 * chore(learn): audit functional programming * chore(learn): audit intermidate algorithms * chore(learn): audit js projects * chore(learn): audit object oriented programming * chore(learn): audit regex * fix(learn): remove stray . * fix(learn): string to code * fix(learn): missed some * fix(learn): clarify strings Based on Randy's feedback, clarifies string instances where quotes were removed in favour of back ticks. * fix: apply suggestions - thanks Randy! :) Co-authored-by: Randell Dawson <5313213+RandellDawson@users.noreply.github.com> * fix: non-suggestion comments * chore(learn): remove comments from codes Removes the comments from the description and instruction code blocks to ensure that all relevant information is translatable. * fix: Apply suggestions from code review Co-authored-by: Shaun Hamilton <51722130+ShaunSHamilton@users.noreply.github.com> * fix: revert crowdin fix * Update curriculum/challenges/english/02-javascript-algorithms-and-data-structures/basic-algorithm-scripting/mutations.md Co-authored-by: Randell Dawson <5313213+RandellDawson@users.noreply.github.com> * fix: Apply suggestions from code review Co-authored-by: Shaun Hamilton <51722130+ShaunSHamilton@users.noreply.github.com> * Update curriculum/challenges/english/02-javascript-algorithms-and-data-structures/es6/use-destructuring-assignment-to-assign-variables-from-arrays.md Co-authored-by: Shaun Hamilton <51722130+ShaunSHamilton@users.noreply.github.com> * fix: Apply suggestions from code review Co-authored-by: Oliver Eyton-Williams <ojeytonwilliams@gmail.com> Co-authored-by: Shaun Hamilton <51722130+ShaunSHamilton@users.noreply.github.com> * chore: change voice * fix: Christopher Nolan * fix: expressions would evaluate * fix: will -> would * Update curriculum/challenges/english/02-javascript-algorithms-and-data-structures/object-oriented-programming/add-methods-after-inheritance.md Co-authored-by: Randell Dawson <5313213+RandellDawson@users.noreply.github.com> * fix: to work to push * Update curriculum/challenges/english/02-javascript-algorithms-and-data-structures/basic-javascript/iterate-with-javascript-for-loops.md Co-authored-by: Shaun Hamilton <51722130+ShaunSHamilton@users.noreply.github.com> * Update curriculum/challenges/english/02-javascript-algorithms-and-data-structures/object-oriented-programming/add-methods-after-inheritance.md Co-authored-by: Randell Dawson <5313213+RandellDawson@users.noreply.github.com> Co-authored-by: Randell Dawson <5313213+RandellDawson@users.noreply.github.com> Co-authored-by: Shaun Hamilton <51722130+ShaunSHamilton@users.noreply.github.com> Co-authored-by: Oliver Eyton-Williams <ojeytonwilliams@gmail.com>
2.3 KiB
id, title, challengeType, forumTopicId, dashedName
id | title | challengeType | forumTopicId | dashedName |
---|---|---|---|---|
587d78b2367417b2b2512b0f | Remove Items from an Array with pop() and shift() | 1 | 301165 | remove-items-from-an-array-with-pop-and-shift |
--description--
Both push()
and unshift()
have corresponding methods that are nearly functional opposites: pop()
and shift()
. As you may have guessed by now, instead of adding, pop()
removes an element from the end of an array, while shift()
removes an element from the beginning. The key difference between pop()
and shift()
and their cousins push()
and unshift()
, is that neither method takes parameters, and each only allows an array to be modified by a single element at a time.
Let's take a look:
let greetings = ['whats up?', 'hello', 'see ya!'];
greetings.pop();
greetings
would have the value ['whats up?', 'hello']
.
greetings.shift();
greetings
would have the value ['hello']
.
We can also return the value of the removed element with either method like this:
let popped = greetings.pop();
greetings
would have the value []
, and popped
would have the value hello
.
--instructions--
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.
--hints--
popShift(["challenge", "is", "not", "complete"])
should return ["challenge", "complete"]
assert.deepEqual(popShift(['challenge', 'is', 'not', 'complete']), [
'challenge',
'complete'
]);
The popShift
function should utilize the pop()
method
assert.notStrictEqual(popShift.toString().search(/\.pop\(/), -1);
The popShift
function should utilize the shift()
method
assert.notStrictEqual(popShift.toString().search(/\.shift\(/), -1);
--seed--
--seed-contents--
function popShift(arr) {
let popped; // Change this line
let shifted; // Change this line
return [shifted, popped];
}
console.log(popShift(['challenge', 'is', 'not', 'complete']));
--solutions--
function popShift(arr) {
let popped = arr.pop(); // Change this line
let shifted = arr.shift(); // Change this line
return [shifted, popped];
}