* feat(tools): add seed/solution restore script * chore(curriculum): remove empty sections' markers * chore(curriculum): add seed + solution to Chinese * chore: remove old formatter * fix: update getChallenges parse translated challenges separately, without reference to the source * chore(curriculum): add dashedName to English * chore(curriculum): add dashedName to Chinese * refactor: remove unused challenge property 'name' * fix: relax dashedName requirement * fix: stray tag Remove stray `pre` tag from challenge file. Signed-off-by: nhcarrigan <nhcarrigan@gmail.com> Co-authored-by: nhcarrigan <nhcarrigan@gmail.com>
88 lines
1.7 KiB
Markdown
88 lines
1.7 KiB
Markdown
---
|
|
id: 56bbb991ad1ed5201cd392cd
|
|
title: Manipulate Arrays With shift()
|
|
challengeType: 1
|
|
videoUrl: 'https://scrimba.com/c/cRbVETW'
|
|
forumTopicId: 18238
|
|
dashedName: manipulate-arrays-with-shift
|
|
---
|
|
|
|
# --description--
|
|
|
|
`pop()` always removes the last element of an array. What if you want to remove the first?
|
|
|
|
That's where `.shift()` comes in. It works just like `.pop()`, except it removes the first element instead of the last.
|
|
|
|
Example:
|
|
|
|
```js
|
|
var ourArray = ["Stimpson", "J", ["cat"]];
|
|
var removedFromOurArray = ourArray.shift();
|
|
// removedFromOurArray now equals "Stimpson" and ourArray now equals ["J", ["cat"]].
|
|
```
|
|
|
|
# --instructions--
|
|
|
|
Use the `.shift()` function to remove the first item from `myArray`, assigning the "shifted off" value to `removedFromMyArray`.
|
|
|
|
# --hints--
|
|
|
|
`myArray` should now equal `[["dog", 3]]`.
|
|
|
|
```js
|
|
assert(
|
|
(function (d) {
|
|
if (d[0][0] == 'dog' && d[0][1] === 3 && d[1] == undefined) {
|
|
return true;
|
|
} else {
|
|
return false;
|
|
}
|
|
})(myArray)
|
|
);
|
|
```
|
|
|
|
`removedFromMyArray` should contain `["John", 23]`.
|
|
|
|
```js
|
|
assert(
|
|
(function (d) {
|
|
if (
|
|
d[0] == 'John' &&
|
|
d[1] === 23 &&
|
|
typeof removedFromMyArray === 'object'
|
|
) {
|
|
return true;
|
|
} else {
|
|
return false;
|
|
}
|
|
})(removedFromMyArray)
|
|
);
|
|
```
|
|
|
|
# --seed--
|
|
|
|
## --after-user-code--
|
|
|
|
```js
|
|
(function(y, z){return 'myArray = ' + JSON.stringify(y) + ' & removedFromMyArray = ' + JSON.stringify(z);})(myArray, removedFromMyArray);
|
|
```
|
|
|
|
## --seed-contents--
|
|
|
|
```js
|
|
// Setup
|
|
var myArray = [["John", 23], ["dog", 3]];
|
|
|
|
// Only change code below this line
|
|
var removedFromMyArray;
|
|
```
|
|
|
|
# --solutions--
|
|
|
|
```js
|
|
var myArray = [["John", 23], ["dog", 3]];
|
|
|
|
// Only change code below this line
|
|
var removedFromMyArray = myArray.shift();
|
|
```
|