-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathRecursion: Reversed List Index
34 lines (28 loc) · 1.74 KB
/
Recursion: Reversed List Index
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
Write a recursive function that filters the items in an array (given as parameter arr) by positional parity (odd or even),
given as parameter par, starting from the opposite end.
Return an array of items on odd positions (... 5, 3, 1) or
on even positions (... 6, 4, 2) and counting from the last item in the array.
getItemsAt([2, 4, 6, 8, 10], "odd") ➞ [2, 6, 10]
// 2, 6 & 10 occupy the 5th, 3rd and 1st positions from right.
// Odd positions, hence the parity, and from the opposite.
getItemsAt(["E", "D", "A", "B", "I", "T"], "even") ➞ ["E", "A", "I"]
// E, A and I occupy the 6th, 4th and 2nd positions from right.
// Even positions, hence the parity, and from the opposite.
getItemsAt([")", "(", "*", "&", "^", "%", "$", "#", "@", "!"], "even") ➞ [")", "*", ^", "$", "@"]
getItemsAt(["A", "R", "B", "I", "T", "R", "A", "R", "I", "L", "Y"], "even") ➞ ["R", "I", "R", "R", "L"]
Notes
IMPORTANT: You are advised to solve this challenge via a recursive approach, hence, the very purpose of this challenge. You can check the Resources tab about a few topics on recursion.
Arrays are zero-indexed, so, index+1 = position or position-1 = index.
Items in the array may contain duplicates. See example #4.
The last item in the array is always the first item to start a positional count.
The sequence of the items in the resulting array should be retained (i.e. example #1 - 6 should come after 2 and before 10, example #2 - "A" should come after "E" and before "I").
My Solution:
const getItemsAt = (arr, par, count=1) => {
if(arr.length <= 1) return []
if(par === "even") {
count = 2
}
const val = arr.splice(count === 1 ? arr.length - 1 : arr.length - 2, arr.length).slice(0, 1)
count++
return getItemsAt(arr, par, count).concat(val)
}