-
Notifications
You must be signed in to change notification settings - Fork 0
/
challenges-06.test.js
165 lines (119 loc) · 5.85 KB
/
challenges-06.test.js
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
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
'use strict';
/* ------------------------------------------------------------------------------------------------
CHALLENGE 1 - Review
Write a function named getNames that, given an array of people objects, uses map to return an array of names reversed.
For example:
[
{
name: 'lloyd',
age: 32,
shoeSize: 12
},
{
name: 'jamie',
age: 21,
shoeSize: 8
}
]
Returns: ['dyoll', 'eimaj'];
------------------------------------------------------------------------------------------------ */
const getNames = (arr) => arr.map(item => item.name.split('').reverse().join(''));
/* ------------------------------------------------------------------------------------------------
CHALLENGE 2
Write a function that appends ' The end.' to a string, and returns the modified string. The original source string should not be modified.
------------------------------------------------------------------------------------------------ */
const appendTheEnd = (str) => `${str} The end.`;
/* ------------------------------------------------------------------------------------------------
CHALLENGE 3
Write a function that accepts an array and copies the first element to the end of the array. The change should be reflected in the source array that was passed in to the function. That is, the function should modify the array 'in place'.
Do not use a return statement.
For example:
const a = [1, 2, 3];
appendFirstToLast(a);
console.log(a) prints [1, 2, 3, 1]
------------------------------------------------------------------------------------------------ */
const appendFirstToLast = (arr) => arr.push(arr[0]) && arr;
/* ------------------------------------------------------------------------------------------------
CHALLENGE 4
Write a function that accepts an object and an integer as arguments and adds a new property to the object called yearBorn. The value of the yearBorn property should be the integer that was passed in.
The change should be reflected in the source object that was passed in to the function. That is, the function should modify the object 'in place'.
Do not use a return statement.
For example:
const octavia = { fullName: 'Octavia Estelle Butler' };
addBirthYearProperty(octavia, 1947);
console.log(a) prints { fullName: 'Octavia Estelle Butler', yearBorn: 1947 }
------------------------------------------------------------------------------------------------ */
const addBirthYearProperty = (obj, year) => obj.yearBorn = year;
/* ------------------------------------------------------------------------------------------------
CHALLENGE 5 - Stretch Goal
Write a function that accepts an array of people objects and adds a new property called isAuthor to each object in the list. Set the value of the new property to true.
The function should modify the object in place. Do not use a return statement.
For example:
const people = [{ fullName: 'Octavia Butler' }, { fullName: 'Ray Bradbury' }];
setStatusAsAuthor(people);
console.log(people[1].isAuthor) prints true
------------------------------------------------------------------------------------------------ */
const setStatusAsAuthor = (people) => people.forEach(person => person.isAuthor = true);
/* ------------------------------------------------------------------------------------------------
CHALLENGE 6 - Stretch Goal
Write a function that accepts two arrays. Append the values from the second array into the first,
maintaining the ordering.
The function should modify the first array in place. Do not use a return statement.
For example:
const a = [1, 2]; NOTE: If you assign an array to a `const`, you can't re-assign it later, but you can change the values in the array.
const b = [3, 4];
append(a, b);
console.log(a) prints [1, 2, 3, 4]
------------------------------------------------------------------------------------------------ */
const append = (arr1, arr2) => arr1.push.apply(arr1, arr2);
/* ------------------------------------------------------------------------------------------------
TESTS
All the code below will verify that your functions are working to solve the challenges.
DO NOT CHANGE any of the below code.
Run your tests from the console: jest challenges-02.test.js
------------------------------------------------------------------------------------------------ */
describe('Testing challenge 1', () => {
test('It returns an array of names reversed', () => {
expect(getNames([{name:'lloyd', age: 32, shoeSize: 12}, {name:'jamie', age:21, shoeSize: 8}])).toStrictEqual(['dyoll', 'eimaj']);
expect(getNames([])).toStrictEqual([]);
});
});
describe('Testing challenge 2', () => {
test('It should append without modifying the original', () => {
const a = 'This is my story.';
const b = appendTheEnd(a);
expect(a).toStrictEqual('This is my story.');
expect(b).toStrictEqual('This is my story. The end.');
});
});
describe('Testing challenge 3', () => {
test('It should append by modifying the original', () => {
const a = ['Yes', 'it', 'is'];
appendFirstToLast(a);
expect(a).toStrictEqual(['Yes', 'it', 'is', 'Yes']);
});
});
describe('Testing challenge 4', () => {
test('It should add a property to an object', () => {
const a = { fullName: 'Octavia Butler' };
addBirthYearProperty(a, 1947);
expect(a.yearBorn).toStrictEqual(1947);
});
});
describe('Testing challenge 5', () => {
test('It should add a property to every object in an array', () => {
const a = [{ fullName: 'Octavia Butler' }, { fullName: 'Ray Bradbury' }, { fullName: 'Kurt Vonnegut' }];
setStatusAsAuthor(a);
expect(a[0].isAuthor).toStrictEqual(true);
expect(a[1].isAuthor).toStrictEqual(true);
expect(a[2].isAuthor).toStrictEqual(true);
});
});
describe('Testing challenge 6', () => {
test('It should append the second array to the first', () => {
const a = [1, 2, 3, 4];
const b = [5, 6, 7, 8];
append(a, b);
expect(a).toStrictEqual([1, 2, 3, 4, 5, 6, 7, 8]);
});
});