-
Notifications
You must be signed in to change notification settings - Fork 0
/
challenges-01.test.js
208 lines (159 loc) · 7.96 KB
/
challenges-01.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
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
'use strict';
/* ------------------------------------------------------------------------------------------------
CHALLENGE 1
Write a function named `addOne` that takes an array of numbers, and returns a new array of the numbers, incremented by 1.
Use `forEach` to loop over the input array and work with each value. Push the new value into a local array. Return the local array;
------------------------------------------------------------------------------------------------ */
const addOne = (arr) => {
let returnArray = [];
arr.forEach((el) => {
returnArray.push(el + 1);
});
return returnArray;
};
/* ------------------------------------------------------------------------------------------------
CHALLENGE 2
Write a function named `addExclamation` that takes an array of strings, and returns a new array of the same strings with an "!" added to the end.
Use `forEach` to loop over the input array. Modify each string, and add the updated value into a local array. Return the local array;
------------------------------------------------------------------------------------------------ */
const addExclamation = (arr) => {
let returnArray = [];
arr.forEach((el) => {
returnArray.push(el + '!');
});
return returnArray;
};
/* ------------------------------------------------------------------------------------------------
CHALLENGE 3
Write a function named `allUpperCase` that takes an array of strings, and returns a new array of the strings converted to upper case.
Use `forEach` to loop over the input array. The modified strings should each be added into a local array. Return that local array.
------------------------------------------------------------------------------------------------ */
const allUpperCase = (arr) => {
let returnArray = [];
arr.forEach((el) => {
returnArray.push(el.toUpperCase());
});
return returnArray;
};
/* ------------------------------------------------------------------------------------------------
CHALLENGE 4
Write a function named `greeting` that takes in a single string and returns the string in all uppercase letters, and followed by an "!".
Then, write a function named `speaker` that takes in an array of strings and a callback function.
Use `forEach` to build a new array of strings, each string modified by the callback. Return the new array.
------------------------------------------------------------------------------------------------ */
const greeting = (word) => {
return `${word.toUpperCase()}!`;
};
const speaker = (words, callback) => {
let returnArray = [];
words.forEach(el => {
returnArray.push(callback(el));
});
return returnArray;
};
/* ------------------------------------------------------------------------------------------------
CHALLENGE 5
Write a function named addValues that takes in an array and a value and pushes the value into the array. This function does not need a return statement.
Then, write a function named addNumbers that takes in four arguments:
- A number to be added to an array
- An array into which the number should be added
- The number of times the number should be added
- A callback function to use to add the numbers to the array (Hint: you already defined it)
Within the addNumbers function, invoke the callback function as many times as necessary, based on the third argument of the addNumbers function.
Return the modified array.
------------------------------------------------------------------------------------------------ */
const addValues = (arr, value) => {arr.push(value);};
const addNumbers = (num, arr, times, callback) => { for (let i = 0; i < times; i++) callback(arr, num); return arr;};
/* ------------------------------------------------------------------------------------------------
CHALLENGE 6
Write a function named createList that takes in an array of the current store intentory.
The inventory is formatted like this:
[
{ name: 'apples', available: true },
{ name: 'pears', available: true },
{ name: 'oranges', available: false },
{ name: 'bananas', available: true },
{ name: 'blueberries', available: false }
]
This function should use forEach to populate your grocery list based on the store's inventory. If the item is available, add it to your list. Return the final list.
------------------------------------------------------------------------------------------------ */
const createList = (availableItems) => {
let returnList = [];
availableItems.forEach(item => {
if(item.available) {
returnList.push(item.name);
}
});
return returnList;
};
/* ------------------------------------------------------------------------------------------------
STRETCH - CHALLENGE 7
Write a function named fizzbuzz that takes in an array of numbers.
Iterate over the array using forEach to determine the output based on several rules:
- If a number is divisible by 3, add the word "Fizz" to the output array.
- If the number is divisible by 5, add the word "Buzz" to the output array.
- If the number is divisible by both 3 and 5, add the phrase "Fizz Buzz" to the output array.
- Otherwise, add the number to the output array.
Return the resulting output array.
------------------------------------------------------------------------------------------------ */
const fizzbuzz = (arr) => {
let returnArray = [];
arr.forEach(item => {
if(item % 3 === 0 && item % 5 === 0){
returnArray.push('Fizz Buzz');
} else if (item % 3 === 0) {
returnArray.push('Fizz');
} else if (item % 5 === 0) {
returnArray.push('Buzz');
} else {
returnArray.push(item);
}
});
return returnArray;
};
/* ------------------------------------------------------------------------------------------------
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-01.test.js
------------------------------------------------------------------------------------------------ */
describe('Testing challenge 1', () => {
test('It should return an array with 1 added to each value of the original array', () => {
expect(addOne([1, 2, 3, 4, 5])).toStrictEqual([2, 3, 4, 5, 6]);
});
});
describe('Testing challenge 2', () => {
test('It should return an array with an exclamation point added to each value of the original array', () => {
expect(addExclamation(['hi', 'how', 'are', 'you'])).toStrictEqual(['hi!', 'how!', 'are!', 'you!']);
});
});
describe('Testing challenge 3', () => {
test('It should return an array of uppercase strings', () => {
expect(allUpperCase(['hi', 'how', 'are', 'you'])).toStrictEqual(['HI', 'HOW', 'ARE', 'YOU']);
});
});
describe('Testing challenge 4', () => {
test('It should provide an array of strings, that get uppercased, and a "!" at the end', () => {
expect(speaker(['hello', '301', 'students'], greeting)).toStrictEqual(['HELLO!', '301!', 'STUDENTS!']);
});
});
describe('Testing challenge 5', () => {
test('It should add the number 8 to the array five times', () => {
expect(addNumbers(8, [], 5, addValues)).toStrictEqual([8, 8, 8, 8, 8]);
expect(addNumbers(8, [], 5, addValues).length).toStrictEqual(5);
});
});
describe('Testing challenge 6', () => {
const inventory = [{ name: 'apples', available: true }, { name: 'pears', available: true }, { name: 'oranges', available: false }, { name: 'bananas', available: true }, { name: 'blueberries', available: false }];
test('It should only add the available items to the list', () => {
expect(createList(inventory)).toStrictEqual(['apples', 'pears', 'bananas']);
expect(createList(inventory).length).toStrictEqual(3);
});
});
describe('Testing challenge 7', () => {
const inputs = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16];
test('It should print out messages or numbers', () => {
expect(fizzbuzz(inputs)).toStrictEqual([1, 2, 'Fizz', 4, 'Buzz', 'Fizz', 7, 8, 'Fizz', 'Buzz', 11, 'Fizz', 13, 14, 'Fizz Buzz', 16]);
expect(fizzbuzz(inputs).length).toStrictEqual(16);
});
});