slicer notation javascript

Solutions on MaxInterview for slicer notation javascript by the best coders in the world

showing results for - "slicer notation javascript"
Magdalena
18 Apr 2018
1const animals = ['ant', 'bison', 'camel', 'duck', 'elephant'];
2
3console.log(animals.slice(2)); //start from index 2
4// expected output: Array ["camel", "duck", "elephant"]
5
6console.log(animals.slice(2, 4)); //start from index 2 until index 4
7// expected output: Array ["camel", "duck"]
8
9console.log(animals.slice(1, 5)); //start from index 1 until index 5 (max index + 1 is acceptable)
10// expected output: Array ["bison", "camel", "duck", "elephant"]
11
12console.log(animals.slice(-2)); //start at the second last value
13// expected output: Array ["duck", "elephant"]
14
15console.log(animals.slice(2, -1)); //go from index to until the last value
16// expected output: Array ["camel", "duck"]