-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsplitList.ts
More file actions
31 lines (24 loc) · 884 Bytes
/
splitList.ts
File metadata and controls
31 lines (24 loc) · 884 Bytes
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
/*
LEVEL: simple*
You have to split a given Array into two Arrays inside an Array.
If input sequence has an odd amount of elements, then the first Array inside result Array should have more elements.
If input sequence has no elements, then two empty Arrays inside result Array should be returned.
Input: An Array.
Output: An Array of two Arrays.
Examples:
splitList([1, 2, 3, 4, 5, 6]) === [[1, 2, 3],[4, 5, 6]]
splitList([1, 2, 3, 4, 5]) === [[1, 2, 3], [4, 5]]
splitList([1, 2, 3]) === [[1, 2], [3]];
splitList([1]) === [[1], []]
splitList([]) === [[], []]
*/
//Answer//
function splitList(values: number[]): number[][] {
const largo = Number(values.length) //6
let copy1: number[] = []
let copy2: number[] = []
if (largo == 0) return [[], []]
copy1 = values.slice(0, Math.ceil(largo / 2))
copy2 = values.slice(Math.ceil(largo / 2))
return [copy1, copy2]
}