Files
freeCodeCamp/curriculum/challenges/german/02-javascript-algorithms-and-data-structures/basic-javascript/manipulate-arrays-with-shift.md
2023-02-16 12:04:56 +01:00

1.9 KiB

id, title, challengeType, videoUrl, forumTopicId, dashedName
id title challengeType videoUrl forumTopicId dashedName
56bbb991ad1ed5201cd392cd Arrays mit der shift-Methode manipulieren 1 https://scrimba.com/c/cRbVETW 18238 manipulate-arrays-with-shift

--description--

pop() entfernt immer das letzte Element eines Arrays. Was, wenn du das erste entfernen möchtest?

Das ist der Moment, in dem .shift() ins Spiel kommt. Es funktioniert genau wie .pop(), nur dass es das erste Element entfernt, anstatt das letzte.

Beispiel:

const ourArray = ["Stimpson", "J", ["cat"]];
const removedFromOurArray = ourArray.shift();

removedFromOurArray hätte einen Wert des Strings Stimpson, und ourArray hätte ["J", ["cat"]].

--instructions--

Benutze die Funktion .shift(), um das erste Element aus myArray zu entfernen und weise dem entfernten Wert einer neue Variable removedFromMyArray zu.

--hints--

myArray sollte nun gleich [["dog", 3]] sein.

assert(
  (function (d) {
    if (d[0][0] == 'dog' && d[0][1] === 3 && d[1] == undefined) {
      return true;
    } else {
      return false;
    }
  })(myArray)
);

removedFromMyArray sollte ["John", 23] enthalten.

assert(
  (function (d) {
    if (
      d[0] == 'John' &&
      d[1] === 23 &&
      typeof removedFromMyArray === 'object'
    ) {
      return true;
    } else {
      return false;
    }
  })(removedFromMyArray)
);

--seed--

--after-user-code--

if (typeof removedFromMyArray !== 'undefined') (function(y, z){return 'myArray = ' + JSON.stringify(y) + ' & removedFromMyArray = ' + JSON.stringify(z);})(myArray, removedFromMyArray);

--seed-contents--

// Setup
const myArray = [["John", 23], ["dog", 3]];

// Only change code below this line

--solutions--

const myArray = [["John", 23], ["dog", 3]];

// Only change code below this line
const removedFromMyArray = myArray.shift();