* 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>
78 lines
1.6 KiB
Markdown
78 lines
1.6 KiB
Markdown
---
|
|
id: 56bbb991ad1ed5201cd392ce
|
|
title: Manipulate Arrays With unshift()
|
|
challengeType: 1
|
|
videoUrl: 'https://scrimba.com/c/ckNDESv'
|
|
forumTopicId: 18239
|
|
dashedName: manipulate-arrays-with-unshift
|
|
---
|
|
|
|
# --description--
|
|
|
|
Not only can you `shift` elements off of the beginning of an array, you can also `unshift` elements to the beginning of an array i.e. add elements in front of the array.
|
|
|
|
`.unshift()` works exactly like `.push()`, but instead of adding the element at the end of the array, `unshift()` adds the element at the beginning of the array.
|
|
|
|
Example:
|
|
|
|
```js
|
|
var ourArray = ["Stimpson", "J", "cat"];
|
|
ourArray.shift(); // ourArray now equals ["J", "cat"]
|
|
ourArray.unshift("Happy");
|
|
// ourArray now equals ["Happy", "J", "cat"]
|
|
```
|
|
|
|
# --instructions--
|
|
|
|
Add `["Paul",35]` to the beginning of the `myArray` variable using `unshift()`.
|
|
|
|
# --hints--
|
|
|
|
`myArray` should now have \[["Paul", 35], ["dog", 3]].
|
|
|
|
```js
|
|
assert(
|
|
(function (d) {
|
|
if (
|
|
typeof d[0] === 'object' &&
|
|
d[0][0] == 'Paul' &&
|
|
d[0][1] === 35 &&
|
|
d[1][0] != undefined &&
|
|
d[1][0] == 'dog' &&
|
|
d[1][1] != undefined &&
|
|
d[1][1] == 3
|
|
) {
|
|
return true;
|
|
} else {
|
|
return false;
|
|
}
|
|
})(myArray)
|
|
);
|
|
```
|
|
|
|
# --seed--
|
|
|
|
## --after-user-code--
|
|
|
|
```js
|
|
(function(y, z){return 'myArray = ' + JSON.stringify(y);})(myArray);
|
|
```
|
|
|
|
## --seed-contents--
|
|
|
|
```js
|
|
// Setup
|
|
var myArray = [["John", 23], ["dog", 3]];
|
|
myArray.shift();
|
|
|
|
// Only change code below this line
|
|
```
|
|
|
|
# --solutions--
|
|
|
|
```js
|
|
var myArray = [["John", 23], ["dog", 3]];
|
|
myArray.shift();
|
|
myArray.unshift(["Paul", 35]);
|
|
```
|