Skip to content

Commit d2704fe

Browse files
committed
starter-code-with-tests
1 parent 0875086 commit d2704fe

13 files changed

+6784
-308
lines changed

.eslintrc.json

+16
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
{
2+
"plugins": [ "jasmine" ],
3+
"env": {
4+
"jasmine": true
5+
},
6+
"extends" : ["airbnb-base/legacy", "plugin:jasmine/recommended"],
7+
"rules": {
8+
"func-names": "off",
9+
"no-alert": "off",
10+
"no-console": "off",
11+
"no-plusplus": "off",
12+
"no-unused-vars": "warn",
13+
"vars-on-top": "off",
14+
"no-multi-spaces": "off"
15+
}
16+
}

README.md

+115-147
Original file line numberDiff line numberDiff line change
@@ -6,16 +6,13 @@
66

77
In this exercise you will apply:
88

9-
- Array iteration tecniques
9+
- Array iteration techniques
1010
- Using functions to manipulate and transform arrays
1111

12-
1312
## Introduction
1413

1514
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.
1615

17-
Let's put this into practice. Use [https://repl.it/](https://repl.it/) to complete the following exercises with your partner.
18-
1916
## Requirements
2017

2118
- [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
2724
Upon completion, run the following commands
2825
```
2926
$ git add .
30-
$ git commit -m "done"
27+
$ git commit -m 'done'
3128
$ git push origin master
3229
```
3330
Navigate to your repo and create a Pull Request -from your master branch to the original repository master branch.
3431

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 '-'
3633

37-
## Deliverables
34+
## Testing Introduction
3835

39-
A single JavaScript file will be enough for now.
36+
### What is testing?
4037

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

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

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

47-
```javascript
48-
function maxOfTwoNumbers(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+
![Jasmine Logo](https://i.imgur.com/A1pop7h.png)
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:
5157

52-
var largest = maxOfTwoNumbers(2, 6);
53-
console.log(largest);
54-
// 6
5558
```
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:
75+
76+
![image](https://user-images.githubusercontent.com/23629340/33389609-c2f3965c-d533-11e7-9a03-e0a89314dd98.png)
77+
78+
**Pass the tests**
79+
80+
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.
5693

5794
## Finding Longest Word
5895

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

6198
**Starter Code**
6299

63100
```javascript
64101
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'
72109
];
73-
74-
var longest = findLongestWord(words);
75-
console.log(longest);
76-
// crocodile
77110
```
78111

79112
## Calculating a Sum
@@ -82,21 +115,12 @@ Calculating a sum is as simple as iterating over an array and adding each of the
82115

83116
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.
84117

85-
Calculate the sum of the following array:
86-
87-
```javascript
88-
var numbers = [6, 12, 1, 18, 13, 16, 2, 1, 8, 10];
89-
```
118+
Create a `sumArray` function that takes an array of numbers as a parameter, and calculate the sum of all its numbers:
90119

91120
**Starter Code**
92121

93122
```javascript
94-
function sumArray(array){
95-
// Your Code here
96-
}
97-
98-
var total = sumArray(numbers);
99-
// 87
123+
var numbers = [6, 12, 1, 18, 13, 16, 2, 1, 8, 10];
100124
```
101125

102126
## Calculate the Average
@@ -108,156 +132,109 @@ Calculating an average is an extremely common task. Let's practice it a bit.
108132
1. Find the sum as we did in the first exercise
109133
2. Take the sum from step 1, and divide it by the number of elements in the list.
110134

111-
112135
### Level 1: Array of Numbers
113136

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

120139
**Starter Code**
121140

122141
```javascript
123-
function averageNumbers(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];
130143
```
131144

132145
### Level 2: Array of Strings
133146

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

151149
**Starter Code**
152150

153151
```javascript
154-
function averageWordLength(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+
];
161164
```
162165

163166
## Unique Arrays
164167

165168
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.
166169

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

169172
**Starter Code**
170173

171174
```javascript
172-
function uniquifyArray(array){
173-
// Your code here
174-
}
175-
176175
var words = [
177-
"crab",
178-
"poison",
179-
"contagious",
180-
"simple",
181-
"bring",
182-
"sharp",
183-
"playground",
184-
"poison",
185-
"communion",
186-
"simple",
187-
"bring"
176+
'crab',
177+
'poison',
178+
'contagious',
179+
'simple',
180+
'bring',
181+
'sharp',
182+
'playground',
183+
'poison',
184+
'communion',
185+
'simple',
186+
'bring'
188187
];
189188

190-
var uniqued = uniquifyArray(words);
191-
console.log(uniqued);
192-
// ["crab", "poison", "contagious", "simple", "bring", "sharp", "playground", "communion"]
193189
```
194190

195191
## Finding Elements
196192

197193
Let's create a simple array search.
198194

199-
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. :)
200196

201197
**Starter Code**
202-
203198
```javascript
204199
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'
213208
];
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
222209
```
223210

224211
## Counting Repetion
225212

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

228215
**Starter Code**
229216

230217
```javascript
231218
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'
243230
];
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
252231
```
253232

254233
## Bonus Quest
255234

256235
In the 20×20 grid below; What is the greatest product of four adjacent numbers in the same direction (up, down, left, right)?
257236

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

262239
```javascript
263240
var matrix = [
@@ -282,13 +259,4 @@ var matrix = [
282259
[20,73,35,29,78,31,90,01,74,31,49,71,48,86,81,16,23,57,05,54],
283260
[01,70,54,71,83,51,54,69,16,92,33,48,61,43,52,01,89,19,67,48],
284261
];
285-
286-
var maxProduct = greatestProduct(matrix);
287-
console.log(maxProduct);
288-
// => 57,148,146
289-
```
290-
291-
## Extra Resources
292-
293-
- [Functions - Mozilla Developer Network](https://developer.mozilla.org/en/docs/Web/JavaScript/Guide/Functions)
294-
- [Arrays - MDN](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array)
262+
```

0 commit comments

Comments
 (0)