mirror of
https://github.com/freeCodeCamp/freeCodeCamp.git
synced 2026-01-04 00:05:28 -05:00
1.6 KiB
1.6 KiB
id, title, challengeType, videoUrl, forumTopicId, dashedName
| id | title | challengeType | videoUrl | forumTopicId | dashedName |
|---|---|---|---|---|---|
| 56bbb991ad1ed5201cd392ce | Manipular arrays com unshift() | 1 | https://scrimba.com/c/ckNDESv | 18239 | manipulate-arrays-with-unshift |
--description--
Não só você pode remover elementos do início de um array, como você também pode adicionar (unshift) elementos ao início de um array i.e. adiciona elementos na frente do array.
.unshift() funciona exatamente como .push(), mas ao invés de adicionar o elemento ao final do array, unshift adiciona o elemento no início do array.
Exemplo:
var ourArray = ["Stimpson", "J", "cat"];
ourArray.shift();
ourArray.unshift("Happy");
Após o shift, ourArray teria o valor ["J","cat"]. Após o unshift, ourArray teria o valor ["Happy","J","cat"].
--instructions--
Adicione ["Paul",35] ao início da variável myArray usando unshift().
--hints--
myArray agora deve ter [["Paul", 35], ["dog", 3]].
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--
(function(y, z){return 'myArray = ' + JSON.stringify(y);})(myArray);
--seed-contents--
// Setup
var myArray = [["John", 23], ["dog", 3]];
myArray.shift();
// Only change code below this line
--solutions--
var myArray = [["John", 23], ["dog", 3]];
myArray.shift();
myArray.unshift(["Paul", 35]);