Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 92d734b

Browse files
committed
Change week 3 step2-6 to flatten an array
1 parent 102d8a9 commit 92d734b

File tree

3 files changed

+22
-24
lines changed

3 files changed

+22
-24
lines changed

Week3/MAKEME.md

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -112,8 +112,13 @@ const arr2d = [[1, 2], [3, 4], [5, 6]];
112112

113113
(for math people you can think of this as a matrix)
114114

115-
How would you print all the items of an array with 3 dimensions?
116-
How about with _K_ dimensions?
115+
How would you flatten out all the items of an array with 2 dimensions into a one-dimensional array? Flattening out the `arr2d` array above would result in:
116+
117+
```js
118+
const flattenedArr = [1, 2, 3, 4, 5, 6];
119+
```
120+
121+
How about 3 dimensions? How about with _K_ dimensions?
117122
What if you didn't know how deep the array was nested? (You don't have to write code for this but think about it.)
118123

119124
**2.7** Here are two functions that look like they do the something similar but they print different results. Please explain what's going on here.

Week3/homework/step2-6.js

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,21 +3,21 @@
33
const arr2d = [[1, 2], [3, 4], [5, 6]];
44
const arr3d = [[[1, 2], [3, 4]], [[5, 6], [7, 8]]];
55

6-
function printArray2d(arr) {
6+
function flattenArray2d(arr) {
77
// Replace this comment and the next line with your code
88
console.log(arr);
99
}
1010

11-
function printArray3d(arr) {
11+
function flattenArray3d(arr) {
1212
// Replace this comment and the next line with your code
1313
console.log(arr);
1414
}
1515

16-
printArray2d(arr2d);
17-
printArray3d(arr3d);
16+
console.log(flattenArray2d(arr2d)); // -> [1, 2, 3, 4, 5, 6]
17+
console.log(flattenArray3d(arr3d)); // -> [1, 2, 3, 4, 5, 6, 7, 8]
1818

1919
// Do not change or remove anything below this line
2020
module.exports = {
21-
printArray2d,
22-
printArray3d
21+
flattenArray2d,
22+
flattenArray3d
2323
};

Week3/test/step2-6.test.js

Lines changed: 9 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,20 @@
11
/* eslint-disable no-return-assign, dot-notation */
2-
const { printArray2d, printArray3d } = require('../homework/step2-6');
2+
const { flattenArray2d, flattenArray3d } = require('../homework/step2-6');
33

44
const arr2d = [[1, 2], [3, 4], [5, 6]];
5-
const expected2d = [1, 2, 3, 4, 5, 6].join('');
5+
const expected2d = [1, 2, 3, 4, 5, 6];
66

77
const arr3d = [[[1, 2], [3, 4]], [[5, 6], [7, 8]]];
8-
const expected3d = [1, 2, 3, 4, 5, 6, 7, 8].join('');
8+
const expected3d = [1, 2, 3, 4, 5, 6, 7, 8];
99

1010
describe('step2-6', () => {
11-
let outputData;
12-
const storeLog = (...inputs) => (outputData += inputs.join(' '));
13-
14-
test('printArray2d -> 1, 2, 3, 4, 5, 6', () => {
15-
outputData = '';
16-
console['log'] = jest.fn(storeLog);
17-
printArray2d(arr2d);
18-
expect(outputData).toBe(expected2d);
11+
test('flattenArray2d -> [1, 2, 3, 4, 5, 6]', () => {
12+
const result = flattenArray2d(arr2d);
13+
expect(result).toEqual(expected2d);
1914
});
2015

21-
test('printArray3d -> 1, 2, 3, 4, 5, 6, 7, 8', () => {
22-
outputData = '';
23-
console['log'] = jest.fn(storeLog);
24-
printArray3d(arr3d);
25-
expect(outputData).toBe(expected3d);
16+
test('flattenArray3d -> [1, 2, 3, 4, 5, 6, 7, 8]', () => {
17+
const result = flattenArray3d(arr3d);
18+
expect(result).toEqual(expected3d);
2619
});
2720
});

0 commit comments

Comments
 (0)