You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Merge pull request #1456 from ironhack-labs/tests-update
Sandra, there was a merge conflict due to the renaming of the test spec file, I resolved it locally.
I like the addition of the bonus iterations. I find both a considerably harder than the rest. I added an extra bit of clarification regarding how values of different types should be added up.
Do you think we should instruct students to not do them in the normal flow of the lab, and only tackle them at the end? Could we ponder moving them to the end of the lab?
Additionally, I split the last iteration and passed the requirement to calculate the products diagonally to an extra iteration, #8.1, since my current cohort found it significantly more complex to solve than horizontally and diagonally. Specs don't include any tests for the diagonal product.
I'm merging this the PR into master, but I think that it would be productive to follow up on this Lab and do some additional work on it.
Cheers!
Copy file name to clipboardExpand all lines: README.md
+51-18Lines changed: 51 additions & 18 deletions
Original file line number
Diff line number
Diff line change
@@ -2,7 +2,6 @@
2
2
3
3
# LAB | JS Functions & Arrays
4
4
5
-
6
5
## Introduction
7
6
8
7
Manipulating arrays in code is a very common operation. Whether you're creating a total for a shopping cart, grabbing only the first names out of a list of people, or moving a piece on a chessboard, you're probably going to be modifying or manipulating an array in some way.
@@ -58,26 +57,28 @@ starter-code/
58
57
├── src
59
58
│ └── functions-and-arrays.js
60
59
├── tests
61
-
│ └── FunctionsAndArraysSpec.js
60
+
│ └── functions-and-arrays.spec.js
62
61
└─ SpecRunner.html
63
62
```
64
63
65
64
We will be working with the `functions-and-arrays.js` file inside of the `src` folder. In the `jasmine` folder you can find all of the files that compose Jasmine, that is already linked with the `SpecRunner.html` file.
66
65
67
-
**Run tests**
66
+
#### Run tests
68
67
69
68
Running automated tests with Jasmine is super easy. All you need to do is open the `SpecRunner.html` file in your browser. You will find something similar this:
You should write your code on the `src/functions-and-arrays.js` file. By following the instructions for each iteration, you should go every test and make sure it's _passing_.
74
+
You should write your code on the `src/functions-and-arrays.js` file. While following the instructions for each iteration, you should check every test and make sure it's _passing_, before moving on.
76
75
77
76
Do not rush. You should take your time to carefully read every iteration, and you should address the _breaking_ tests as you progress through the exercise.
78
77
79
78
When coding with tests, it is super important that you carefully read and understand the errors you're getting, this way you'll know for sure what's expected from your code.
80
79
80
+
Note that **you don't need to execute the functions yourself**, the tests are responsible for doing that. All you should do is declare them, make sure they deal with the parameters passed and that they return what is indicated on the iterations and in the test messages. For some iterations we provide you with a sample array, so that you can do some **manual** testing, if you wish.
81
+
81
82
## Deliverables
82
83
83
84
Write your JavaScript in the provided `src/functions-and-arrays.js` file.
@@ -86,38 +87,54 @@ Write your JavaScript in the provided `src/functions-and-arrays.js` file.
86
87
87
88
Define a function `maxOfTwoNumbers` that takes two numbers as arguments and returns the largest.
88
89
89
-
## Iteration #2: Find longest word
90
+
## Iteration #2: Find the longest word
90
91
91
92
Declare a function named `findLongestWord` that takes as an argument an array of words and returns the longest one. If there are 2 with the same length, it should return the first occurrence.
92
93
93
-
**Starter Code**
94
+
You can use the following array to test your solution:
Calculating a sum is as simple as iterating over an array and adding each of the elements together.
102
+
Calculating a sum can be as simple as iterating over an array and adding each of the elements together.
102
103
103
-
<!-- Semantically [reduce](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/Reduce) is the best method to use for this, but you can use any loop we've discussed so far. -->
104
+
Declare a function named `sumArray` that takes an array of numbers as an argument, and returns the sum of all of the numbers in the array. Later in the course we'll learn how to do this by using the `reduce` array method, which will make your work significantly easier. For now, let's practice _"declarative"_ way adding values, using loops.
104
105
105
-
Declare a function named `sumArray` that takes as an argument an array of numbers, and returns the sum of all of the numbers in the array. Later in the course we'll learn how to do this by using the `reduce` array method, which will make your work significantly easier. For now, let's practice _"manual"_ way using loops.
106
-
107
-
**Starter Code**
106
+
You can use the following array to test your solution:
### Bonus - Iteration #3.1: A generic `sum()` function
113
+
114
+
**The goal: Learn how to refactor your code.**:muscle:
115
+
116
+
In the iteration 3, you created a function that returns the sum of an array of numbers. But what if we wanted to know how much is the sum of the length of all of the words in an array? What if we wanted to add _boolean_ values to the mix? We wouldn't be able to use the same function as above, or better saying, we would have to _tweak_ it a little bit so that it can be reused no matter what is in the array that is passed as argument when function `sumArray()` is called.
117
+
118
+
Here we're applying a concept we call **polymorphism**, that is, dealing with a functions' input independently of the types they're passed as.
119
+
120
+
Let's create a new function `sum()` that calculates the sum for array filled with (_almost_) any type of data. Note that strings should have their length added to the total, and boolean values should be coerced into their corresponding numeric values. Check the tests for more details.
121
+
122
+
You can use the following array to test your solution:
Calculating an average is an extremely common task. Let's practice it a bit.
116
133
117
-
**Algorithm**
134
+
**The logic behind this:**
118
135
119
-
1. Find the sum as we did in the first exercise
120
-
2. Take the sum from step 1, and divide it by the number of elements in the list.
136
+
1. Find the sum as we did in the first exercise (or how about reusing that the _sumArray()_?)
137
+
2. Take that sum and divide it by the number of elements in the list.
121
138
122
139
### Level 1: Array of numbers
123
140
@@ -150,6 +167,16 @@ const words = [
150
167
];
151
168
```
152
169
170
+
### Bonus - Iteration #4.1: A generic `avg()` function
171
+
172
+
Create function `avg(arr)` that receives any mixed array and calculates average. Consider as mixed array an array filled with numbers and/or strings and/or booleans. We're following a similar logic to the one applied on the bonus iteration 4.1 :wink:
Take the following array, remove the duplicates, and return a new array. You're more than likely going to want to check out the [`indexOf`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/indexOf) Array method.
@@ -217,21 +244,23 @@ const words = [
217
244
];
218
245
```
219
246
220
-
## Iteration #8: Bonus
247
+
## Bonus - Iteration #8: Product of adjacent numbers
221
248
222
-
What is the greatest product of four adjacent numbers? We consider adjacent any four numbers that are next to each other in horizontal, vertical or diagonal.
249
+
What is the greatest product of four adjacent numbers? We consider adjacent any four numbers that are next to each other horizontally or vertically.
223
250
224
251
For example, if we have a 5x5 Matrix like:
252
+
225
253
```bash
226
254
[ 1, 2, 3, 4, 5]
227
255
[ 1, 20, 3, 4, 5]
228
256
[ 1, 20, 3, 4, 5]
229
257
[ 1, 20, 3, 4, 5]
230
258
[ 1, 4, 3, 4, 5]
231
259
```
260
+
232
261
The greatest product will be the `20`x`20`x`20`x`4` = `32000`;
233
262
234
-
Declare a function named `greatestProduct` to find it in the 20×20 grid below!
263
+
Declare a function named `greatestProduct(matrix)` to find it in the 20×20 grid below!
235
264
236
265
```javascript
237
266
constmatrix= [
@@ -258,4 +287,8 @@ const matrix = [
258
287
];
259
288
```
260
289
290
+
## Bonus - Iteration #8.1: Product of diagonals
291
+
292
+
Following the logic you've used in iteration #8, declare a function called `greatestProductOfDiagonals(matrix)`. It takes a matrix as a parameter and returns the greatest product of any four values layed out diagonally, in either direction.
0 commit comments