Skip to content

Commit 39afd0a

Browse files
authored
Merge pull request ironhack-labs#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, ironhack-labs#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!
2 parents eb98808 + 9278e24 commit 39afd0a

File tree

5 files changed

+432
-354
lines changed

5 files changed

+432
-354
lines changed

README.md

Lines changed: 51 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22

33
# LAB | JS Functions & Arrays
44

5-
65
## Introduction
76

87
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/
5857
├── src
5958
│   └── functions-and-arrays.js
6059
├── tests
61-
│   └── FunctionsAndArraysSpec.js
60+
│   └── functions-and-arrays.spec.js
6261
└─ SpecRunner.html
6362
```
6463

6564
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.
6665

67-
**Run tests**
66+
#### Run tests
6867

6968
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:
7069

7170
![image](https://user-images.githubusercontent.com/23629340/33389609-c2f3965c-d533-11e7-9a03-e0a89314dd98.png)
7271

73-
**Pass the tests**
72+
#### Pass the tests
7473

75-
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.
7675

7776
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.
7877

7978
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.
8079

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+
8182
## Deliverables
8283

8384
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.
8687

8788
Define a function `maxOfTwoNumbers` that takes two numbers as arguments and returns the largest.
8889

89-
## Iteration #2: Find longest word
90+
## Iteration #2: Find the longest word
9091

9192
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.
9293

93-
**Starter Code**
94+
You can use the following array to test your solution:
9495

9596
```javascript
9697
const words = ['mystery', 'brother', 'aviator', 'crocodile', 'pearl', 'orchard', 'crackpot'];
9798
```
9899

99100
## Iteration #3: Calculate the sum
100101

101-
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.
102103

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.
104105

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:
108107

109108
```javascript
110109
const numbers = [6, 12, 1, 18, 13, 16, 2, 1, 8, 10];
111110
```
112111

112+
### 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:
123+
124+
```javascript
125+
const mixedArr = [6, 12, 'miami', 1, true, 'barca', '200', 'lisboa', 8, 10];
126+
127+
// should return: 57
128+
```
129+
113130
## Iteration #4: Calculate the average
114131

115132
Calculating an average is an extremely common task. Let's practice it a bit.
116133

117-
**Algorithm**
134+
**The logic behind this:**
118135

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.
121138

122139
### Level 1: Array of numbers
123140

@@ -150,6 +167,16 @@ const words = [
150167
];
151168
```
152169

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:
173+
174+
```javascript
175+
const mixedArr = [6, 12, 'miami', 1, true, 'barca', '200', 'lisboa', 8, 10];
176+
177+
// should return: 5.7
178+
```
179+
153180
## Iteration #5: Unique arrays
154181

155182
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 = [
217244
];
218245
```
219246

220-
## Iteration #8: Bonus
247+
## Bonus - Iteration #8: Product of adjacent numbers
221248

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.
223250

224251
For example, if we have a 5x5 Matrix like:
252+
225253
```bash
226254
[ 1, 2, 3, 4, 5]
227255
[ 1, 20, 3, 4, 5]
228256
[ 1, 20, 3, 4, 5]
229257
[ 1, 20, 3, 4, 5]
230258
[ 1, 4, 3, 4, 5]
231259
```
260+
232261
The greatest product will be the `20`x`20`x`20`x`4` = `32000`;
233262

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!
235264

236265
```javascript
237266
const matrix = [
@@ -258,4 +287,8 @@ const matrix = [
258287
];
259288
```
260289

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.
293+
261294
**Happy coding!** :heart:

starter-code/SpecRunner.html

Lines changed: 14 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,22 @@
11
<!DOCTYPE html>
22
<html>
3-
<head>
4-
<meta charset="utf-8">
5-
<title>Jasmine Spec Runner v2.8.0</title>
3+
<head>
4+
<meta charset="utf-8" />
5+
<title>Jasmine Spec Runner v2.8.0</title>
66

7-
<link rel="shortcut icon" type="image/png" href="jasmine/jasmine-2.8.0/jasmine_favicon.png">
8-
<link rel="stylesheet" href="jasmine/jasmine-2.8.0/jasmine.css">
7+
<link rel="shortcut icon" type="image/png" href="jasmine/jasmine-2.8.0/jasmine_favicon.png" />
8+
<link rel="stylesheet" href="jasmine/jasmine-2.8.0/jasmine.css" />
99

10-
<script src="jasmine/jasmine-2.8.0/jasmine.js"></script>
11-
<script src="jasmine/jasmine-2.8.0/jasmine-html.js"></script>
12-
<script src="jasmine/jasmine-2.8.0/boot.js"></script>
10+
<script src="jasmine/jasmine-2.8.0/jasmine.js"></script>
11+
<script src="jasmine/jasmine-2.8.0/jasmine-html.js"></script>
12+
<script src="jasmine/jasmine-2.8.0/boot.js"></script>
1313

14-
<!-- include source files here... -->
15-
<script src="src/functions-and-arrays.js"></script>
14+
<!-- include source files here... -->
15+
<script src="src/functions-and-arrays.js"></script>
1616

17-
<!-- include spec files here... -->
18-
<script src="tests/FunctionsAndArraysSpec.js"></script>
17+
<!-- include spec files here... -->
18+
<script src="tests/functions-and-arrays.spec.js"></script>
19+
</head>
1920

20-
</head>
21-
22-
<body>
23-
</body>
21+
<body></body>
2422
</html>

starter-code/src/functions-and-arrays.js

Lines changed: 2 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -12,18 +12,7 @@ const numbers = [6, 12, 1, 18, 13, 16, 2, 1, 8, 10];
1212
const numbersAvg = [2, 6, 9, 10, 7, 4, 1, 9];
1313

1414
// Level 2: Array of strings
15-
const wordsArr = [
16-
'seat',
17-
'correspond',
18-
'linen',
19-
'motif',
20-
'hole',
21-
'smell',
22-
'smart',
23-
'chaos',
24-
'fuel',
25-
'palace'
26-
];
15+
const wordsArr = ['seat', 'correspond', 'linen', 'motif', 'hole', 'smell', 'smart', 'chaos', 'fuel', 'palace'];
2716

2817
// Iteration #5: Unique arrays
2918
const wordsUnique = [
@@ -41,16 +30,7 @@ const wordsUnique = [
4130
];
4231

4332
// Iteration #6: Find elements
44-
const wordsFind = [
45-
'machine',
46-
'subset',
47-
'trouble',
48-
'starting',
49-
'matter',
50-
'eating',
51-
'truth',
52-
'disobedience'
53-
];
33+
const wordsFind = ['machine', 'subset', 'trouble', 'starting', 'matter', 'eating', 'truth', 'disobedience'];
5434

5535
// Iteration #7: Count repetition
5636
const wordsCount = [

0 commit comments

Comments
 (0)