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
Copy file name to clipboardExpand all lines: README.md
+115-147
Original file line number
Diff line number
Diff line change
@@ -6,16 +6,13 @@
6
6
7
7
In this exercise you will apply:
8
8
9
-
- Array iteration tecniques
9
+
- Array iteration techniques
10
10
- Using functions to manipulate and transform arrays
11
11
12
-
13
12
## Introduction
14
13
15
14
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.
16
15
17
-
Let's put this into practice. Use [https://repl.it/](https://repl.it/) to complete the following exercises with your partner.
18
-
19
16
## Requirements
20
17
21
18
-[Fork this repo](https://guides.github.com/activities/forking/)
@@ -27,53 +24,89 @@ Let's put this into practice. Use [https://repl.it/](https://repl.it/) to comple
27
24
Upon completion, run the following commands
28
25
```
29
26
$ git add .
30
-
$ git commit -m "done"
27
+
$ git commit -m 'done'
31
28
$ git push origin master
32
29
```
33
30
Navigate to your repo and create a Pull Request -from your master branch to the original repository master branch.
34
31
35
-
In the Pull request name, add your name and last names separated by a dash "-"
32
+
In the Pull request name, add your name and last names separated by a dash '-'
36
33
37
-
## Deliverables
34
+
## Testing Introduction
38
35
39
-
A single JavaScript file will be enough for now.
36
+
### What is testing?
40
37
41
-
## Find the maximum
38
+
Software testing is a process of executing an application to validate and verify that it meets the business and technical requirements and works as expected.
42
39
43
-
Define a function `maxOfTwoNumbers` that takes two numbers as arguments and returns the largest.
40
+
Testing is a process, not a single activity. So the process of designing tests early at the beginning of the development and the product's lifecycle can help to prevent deficiencies in the code or product design.
44
41
45
-
**Starter Code**
42
+
We have created all the tests you need to create the solution, and you have to execute them all and create the code to accomplish all the requirements.
46
43
47
-
```javascript
48
-
functionmaxOfTwoNumbers(first, second){
49
-
// Your Code Here
50
-
}
44
+
Tests prove that your code actually works in every situation in which it’s designed to work. Even when you are improving the design or creating new features, you can change your current code without breaking what already works.
45
+
46
+
### Testing with Jasmine
47
+
48
+

49
+
50
+
Jasmine is an automated testing framework for JavaScript. It is designed to be used in BDD (behavior-driven development) programming which focuses more on the business value than on the technical details.
51
+
52
+
We have already included Jasmine in the project you just forked, so let's see how to use it to implement our code.
53
+
54
+
### Usage
55
+
56
+
Before start coding, we will explain the project structure we have provided you:
51
57
52
-
var largest =maxOfTwoNumbers(2, 6);
53
-
console.log(largest);
54
-
// 6
55
58
```
59
+
starter-code/
60
+
├── jasmine
61
+
│ ├── jasmine-2.8.0/
62
+
│ | └── ...
63
+
├── src
64
+
│ └── functions-and-arrays.js
65
+
├── tests
66
+
│ └── FunctionsAndArraysSpec.js
67
+
└─ SpecRunner.html
68
+
```
69
+
70
+
We will be working with the `functions-and-arrays.js` file inside the `src` folder. In the `jasmine` folder you can find all the files that compose Jasmine, that is already linked with the `SpecRunner.html` file.
71
+
72
+
**Run tests**
73
+
74
+
Run the tests with Jasmine is super easy, you just have to open the `SpecRunner.html` file in your browser. You will find something like this:
You have to write your code on the `src/functions-and-arrays.js` file. Following the instructions, you should go step by step passing all the tests.
81
+
82
+
Do not rush to go through all of them at once, take your time to read carefully about what the iteration is asking you, and solve the errors one by one.
83
+
84
+
When coding with tests, is super important to read and understand the errors we are having for each test, this way we will know what it expect from your code.
85
+
86
+
## Deliverables
87
+
88
+
All our work will be located in the `functions-and-arrays.js` file, so that will be enough. Anyway, you have to `push` the whole repo to Github.
89
+
90
+
## Find the maximum
91
+
92
+
Define a function `maxOfTwoNumbers` that takes two numbers as arguments and returns the largest.
56
93
57
94
## Finding Longest Word
58
95
59
-
Write a function `findLongestWord` that takes an array of words and returns the length of the longest one.
96
+
Write a function `findLongestWord` that takes an array of words and returns the length of the longest one. If there are 2 with the same length, it should return the first occurrence.
60
97
61
98
**Starter Code**
62
99
63
100
```javascript
64
101
var words = [
65
-
"mystery",
66
-
"brother",
67
-
"aviator",
68
-
"crocodile",
69
-
"pearl",
70
-
"orchard",
71
-
"crackpot"
102
+
'mystery',
103
+
'brother',
104
+
'aviator',
105
+
'crocodile',
106
+
'pearl',
107
+
'orchard',
108
+
'crackpot'
72
109
];
73
-
74
-
var longest =findLongestWord(words);
75
-
console.log(longest);
76
-
// crocodile
77
110
```
78
111
79
112
## Calculating a Sum
@@ -82,21 +115,12 @@ Calculating a sum is as simple as iterating over an array and adding each of the
82
115
83
116
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.
@@ -108,156 +132,109 @@ Calculating an average is an extremely common task. Let's practice it a bit.
108
132
1. Find the sum as we did in the first exercise
109
133
2. Take the sum from step 1, and divide it by the number of elements in the list.
110
134
111
-
112
135
### Level 1: Array of Numbers
113
136
114
-
Write code to calculate the average of the following array:
115
-
116
-
```javascript
117
-
var numbers = [2, 6, 9, 10, 7, 4, 1, 9];
118
-
```
137
+
Write a function `averageNumbers` that receives an array of numbers and calculate the average of the numbers:
119
138
120
139
**Starter Code**
121
140
122
141
```javascript
123
-
functionaverageNumbers(array){
124
-
// Your code here
125
-
}
126
-
127
-
var average =averageNumbers(numbers);
128
-
console.log(average);
129
-
// 6
142
+
var numbers = [2, 6, 9, 10, 7, 4, 1, 9];
130
143
```
131
144
132
145
### Level 2: Array of Strings
133
146
134
-
Write code to calculate the average *length* of the strings inside of the following array:
135
-
136
-
```javascript
137
-
var words = [
138
-
"seat",
139
-
"correspond",
140
-
"linen",
141
-
"motif",
142
-
"hole",
143
-
"smell",
144
-
"smart",
145
-
"chaos",
146
-
"fuel",
147
-
"palace"
148
-
];
149
-
```
147
+
Write a function `averageWordLength` that receives an array of words and calculate the average length of the words:
150
148
151
149
**Starter Code**
152
150
153
151
```javascript
154
-
functionaverageWordLength(array){
155
-
// Your code here
156
-
}
157
-
158
-
var averageLength =averageNumbers(words);
159
-
console.log(averageLength);
160
-
// 5.3
152
+
var words = [
153
+
'seat',
154
+
'correspond',
155
+
'linen',
156
+
'motif',
157
+
'hole',
158
+
'smell',
159
+
'smart',
160
+
'chaos',
161
+
'fuel',
162
+
'palace'
163
+
];
161
164
```
162
165
163
166
## Unique Arrays
164
167
165
168
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) function.
166
169
167
-
Do this in the form of a function.
170
+
Do this in the form of a function`uniquifyArray` that receives an array of words as a parameter.
Write a function that will take in an array of words as one argument, and a word to search for as the other. Return `true` if it exists, otherwise return `false`. **Don't** use `indexOf` for this one. :)
195
+
Write a function `doesWordExist`that will take in an array of words as one argument, and a word to search for as the other. Return `true` if it exists, otherwise, return `false`. **Don't** use `indexOf` for this one. :)
200
196
201
197
**Starter Code**
202
-
203
198
```javascript
204
199
var words = [
205
-
"machine",
206
-
"subset",
207
-
"trouble",
208
-
"starting",
209
-
"matter",
210
-
"eating",
211
-
"truth",
212
-
"disobedience"
200
+
'machine',
201
+
'subset',
202
+
'trouble',
203
+
'starting',
204
+
'matter',
205
+
'eating',
206
+
'truth',
207
+
'disobedience'
213
208
];
214
-
215
-
var hasMatter =doesWordExist(words, "matter");
216
-
console.log(hasMatter);
217
-
// true
218
-
219
-
var hasDog =doesWordExist(words, "dog");
220
-
console.log(hasDog);
221
-
// false
222
209
```
223
210
224
211
## Counting Repetion
225
212
226
-
Write a function that will take in an array of words as one argument, and a word to search for as the other. The function will return the number of times that word appears in the array.
213
+
Write a function `howManyTimes`that will take in an array of words as one argument, and a word to search for as the other. The function will return the number of times that word appears in the array.
227
214
228
215
**Starter Code**
229
216
230
217
```javascript
231
218
var words = [
232
-
"machine",
233
-
"matter",
234
-
"subset",
235
-
"trouble",
236
-
"starting",
237
-
"matter",
238
-
"eating",
239
-
"matter",
240
-
"truth",
241
-
"disobedience"
242
-
"matter"
219
+
'machine',
220
+
'matter',
221
+
'subset',
222
+
'trouble',
223
+
'starting',
224
+
'matter',
225
+
'eating',
226
+
'matter',
227
+
'truth',
228
+
'disobedience'
229
+
'matter'
243
230
];
244
-
245
-
var howManyMatter =howManyTimes(words, "matter");
246
-
console.log(howManyMatter);
247
-
// 4
248
-
249
-
var howManyDog =howManyTimes(words, "dog");
250
-
console.log(howManyDog);
251
-
// 0
252
231
```
253
232
254
233
## Bonus Quest
255
234
256
235
In the 20×20 grid below; What is the greatest product of four adjacent numbers in the same direction (up, down, left, right)?
257
236
258
-
For example for the 08 we have at position [0][0], we will get 98 because we have to multiply 02(right) * 49(down). That´s all because we do not have any number to its left or up.
259
-
260
-
Another example could the 49 that we have at position [1][1], the product will be 475.398 because we will multiply 02(up) * 99(right) * 49(down) * 49(left).
237
+
Write a function `greatestProduct` to find the answer!
0 commit comments