In JavaScript, an array is a list-like structure with no fixed length which can hold any type of primitives or objects, even mixed types.
To create an array, add elements between square brackets [].
To read from the array, put the index in square brackets [] after the identifier.
The indices of an array start at zero.
For example:
const numbers = [1, 'two', 3, 'four'];
numbers[2];
// => 3
To retrieve the number of elements that are in an array, use the length property:
const numbers = [1, 'two', 3, 'four'];
numbers.length;
// => 4
To change an element in the array, you assign a value at the index:
const numbers = [1, 'two', 3, 'four'];
numbers[0] = 'one';
numbers;
// => ['one', 'two', 3, 'four']
Some of the methods that are available on every Array object can be used to add or remove from the array. Here are a few to consider when working on this exercise:
A value can be added to the end of an array by using .push(value).
The method returns the new length of the array.
const numbers = [1, 'two', 3, 'four'];
numbers.push(5); // => 5
numbers;
// => [1, 'two', 3, 'four', 5]
The last value can be removed from an array by using .pop()
The method returns the removed value.
The length of the array will be decreased because of this change.
const numbers = [1, 'two', 3, 'four'];
numbers.pop(); // => four
numbers;
// => [1, 'two', 3]
The first value can be removed from an array by using .shift()
The method returns the removed value.
The length of the array will be decreased because of this change.
const numbers = [1, 'two', 3, 'four'];
numbers.shift(); // => 1
numbers;
// => ['two', 3, 'four']
A value can be added to the beginning of an array by using .unshift(value).
The method returns the new length of the array.
const numbers = [1, 'two', 3, 'four'];
numbers.unshift('one'); // => 5
numbers;
// => ['one', 1, 'two', 3, 'four']
A value at a specific index can be removed from an array by using .splice(index, 1).
The method returns the removed element(s).
const numbers = [1, 'two', 3, 'four'];
numbers.splice(2, 1, 'one'); // => [3]
numbers;
// => [1, 'two', 'one', 'four']
These methods are more powerful than described:
push and unshift allow you to push or unshift multiple values at once, by adding more arguments.
That is not necessary to complete this exercise.deleteCount.
This can be used to replace values, or insert values in the middle of an array (for example by removing 0 elements).
That is not necessary to complete this exercise.As a magician-to-be, Elyse needs to practice some basics. She has a stack of cards that she wants to manipulate.
To make things a bit easier she only uses the cards 1 to 10 so her stack of cards can be represented by an array of numbers. The position of a certain card corresponds to the index in the array. That means position 0 refers to the first card, position 1 to the second card etc.
All but two functions should update the array of cards and then return the modified array - a common way of working known as the Builder pattern, which allows you to nicely daisy-chain functions together.
The two exceptions are getItem, which should return the card at the given position, and checkSizeOfStack which should return true if the given size matches.
To pick a card, return the card at index position from
the given stack.
const position = 2;
getItem([1, 2, 4, 1], position);
// => 4
Perform some sleight of hand and exchange the card at index position
with the replacement card provided.
Return the adjusted stack.
const position = 2;
const replacementCard = 6;
setItem([1, 2, 4, 1], position, replacementCard);
// => [1, 2, 6, 1]
Make a card appear by inserting a new card at the top of the stack. Return the adjusted stack.
const newCard = 8;
insertItemAtTop([5, 9, 7, 1], newCard);
// => [5, 9, 7, 1, 8]
Make a card disappear by removing the card at the given position from the stack.
Return the adjusted stack.
const position = 2;
removeItem([3, 2, 6, 4, 8], position);
// => [3, 2, 4, 8]
Make a card disappear by removing the card at the top of the stack. Return the adjusted stack.
removeItemFromTop([3, 2, 6, 4, 8]);
// => [3, 2, 6, 4]
Make a card appear by inserting a new card at the bottom of the stack. Return the adjusted stack.
const newCard = 8;
insertItemAtBottom([5, 9, 7, 1], newCard);
// => [8, 5, 9, 7, 1]
Make a card disappear by removing the card at the bottom of the stack. Return the adjusted stack.
removeItemAtBottom([8, 5, 9, 7, 1]);
// => [5, 9, 7, 1]
Check whether the size of the stack is equal to stackSize or not.
const stackSize = 4;
checkSizeOfStack([3, 2, 6, 4, 8], stackSize);
// => false
Sign up to Exercism to learn and master JavaScript with 37 concepts159 exercises, and real human mentoring, all for free.