I want to start at index of "D" and go back up to index of "B" using the slice method
let letters = ["A", "B", "C", "D", "E", "F", "G", "H", "I","J"];
I’m expecting to get back
["D","C","B"]
I want to start at index of "D" and go back up to index of "B" using the slice method
let letters = ["A", "B", "C", "D", "E", "F", "G", "H", "I","J"];
I’m expecting to get back
["D","C","B"]
3
Answers
Slice cant to go back
If you want to get ["D", "B","C"]
If you want to get ["B", "C", "D"]
From what I understand, you want the first element in your final array to be "D", and then you want the elements in your slice to come after "D", starting with "B". The spread operator
...
can be used to expand the slice into a new array that also contains "D".Here is another attempt, using the Array methods
.reverse()
and.reduce()
: