1
- const arrayToTransform = [ 1 , 2 , 3 ]
2
- const listToTransform = {
3
- value : 1 ,
4
- rest : {
5
- value : 2 ,
6
- rest : {
7
- value : 3 ,
8
- rest : null ,
9
- } ,
10
- } ,
11
- }
12
-
13
- // Part 1
14
- const arrayToList = array => {
1
+ export const arrayToList = array => {
15
2
let list = { }
16
3
const lastIndex = array . length - 1
17
4
@@ -25,10 +12,8 @@ const arrayToList = array => {
25
12
26
13
return list
27
14
}
28
- console . log ( arrayToList ( arrayToTransform ) ) // => { value: 1, rest: { value: 2, rest: { value: 3, rest: null }}}
29
15
30
- // Using while
31
- const listToArray = list => {
16
+ export const listToArray = list => {
32
17
let initialElement = list
33
18
const array = [ ]
34
19
@@ -39,21 +24,16 @@ const listToArray = list => {
39
24
40
25
return array
41
26
}
42
- console . log ( listToArray ( listToTransform ) ) // [1, 2, 3]
43
27
44
- // Part 2
45
- const prepend = ( element , list ) => {
28
+ export const prepend = ( element , list ) => {
46
29
return {
47
30
value : element ,
48
31
rest : list ,
49
32
}
50
33
}
51
- console . log ( prepend ( 0 , listToTransform ) ) // => { value: 0, rest: { value: 1, rest: [Object] }}
52
34
53
- // Get an element from the list, recursively.
54
- const nth = ( list , number ) => {
35
+ export const nth = ( list , number ) => {
55
36
if ( ! list ) return 'Element does not exist.'
56
37
57
38
return number - 1 === 0 && list !== null ? list : nth ( list . rest , number - 1 )
58
39
}
59
- console . log ( nth ( listToTransform , 3 ) ) // => { value: 3, rest: null }
0 commit comments