Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
reiterate on the bonus iteration for calculatin the sum of mixed array
  • Loading branch information
sandrabosk committed Jan 24, 2020
commit c18234fda61dbfb704f2ef6eafd78302af671696
16 changes: 12 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -107,10 +107,12 @@ You can use the following array to test your solution:
const numbers = [6, 12, 1, 18, 13, 16, 2, 1, 8, 10];
```

### Bonus - Iteration #3.1: Calculate the sum for array filled with (_almost_) any type of data
### Bonus - Iteration #3.1: A generic `sum()` function

**The goal: Learn how to refactor your**
In the iteration 3, you created a function that will return a sum of array of numbers. But what if we want to know how much is a sum of some array of words? 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 it can be reused no matter what is in the array that is passed as argument when function `sumArray` is called.
**The goal: Learn how to refactor your code.** :muscle:
In the iteration 3, you created a function that will return a sum of array of numbers. But what if we want to know how much is a sum of some array of words? 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 it can be reused no matter what is in the array that is passed as argument when function _sumArray()_ is called.

Let's create a new function `sum()` that calculates the sum for array filled with (_almost_) any type of data. Check the tests for more details.

You can use the following array to test your solution:

Expand Down Expand Up @@ -151,7 +153,13 @@ const words = ['seat', 'correspond', 'linen', 'motif', 'hole', 'smell', 'smart',

### Bonus - Iteration #4.1: A generic `avg()` function

Create function `avg(arr)` that receive any array filled with numbers and/or strings and calculates average.
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.

```javascript
const mixedArr = [6, 12, 'miami', 1, 'barca', '200', 'lisboa', 8, 10];

// should return: 6.22
```

## Iteration #5: Unique arrays

Expand Down
116 changes: 63 additions & 53 deletions starter-code/tests/functions-and-arrays.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -57,52 +57,68 @@ describe('Find the longest word', () => {
});
});

describe('Calculate the sum', () => {
it('should create a function named sumArray', () => {
expect(typeof sumArray).toBe('function');
describe('Calculate the sum of array of numbers', () => {
it('should create a function named sumNumbers', () => {
expect(typeof sumNumbers).toBe('function');
});

it('should return zero if receives an empty array when called', () => {
expect(sumArray([])).toBe(0);
expect(sumNumbers([])).toBe(0);
});

it('should return the sum with one number array', () => {
expect(sumArray([4])).toBe(4);
expect(sumNumbers([4])).toBe(4);
});

it('should return zero if all elements are zero', () => {
expect(sumArray([0, 0, 0, 0, 0])).toBe(0);
expect(sumNumbers([0, 0, 0, 0, 0])).toBe(0);
});

it('should return the sum when passed array of numbers', () => {
expect(sumArray([10, 5, 4, 32, 8])).toBe(59);
expect(sumNumbers([10, 5, 4, 32, 8])).toBe(59);
});
});

it('bonus - should return the sum when passed array of strings', () => {
expect(sumArray(['ana', 'marco', 'nicolas', 'tania', 'ptwd'])).toBe(24);
describe('Bonus: Calculate the sum', () => {
it('should create a function named sum', () => {
expect(typeof sum).toBe('function');
});

it('bonus - should return the sum when passed array of mixed strings and numbers - ', () => {
expect(sumArray([6, 12, 'miami', 1, 'barca', '200', 'lisboa', 8, 10])).toBe(
56
);
it('should return zero if receives an empty array when called', () => {
expect(sum([])).toBe(0);
});

it('should return the sum with one number array', () => {
expect(sum([4])).toBe(4);
});

it('should return zero if all elements are zero', () => {
expect(sum([0, 0, 0, 0, 0])).toBe(0);
});
it('bonus - should return the sum when passed array of mixed strings, numbers and booleans - ', () => {

it('should return the sum when passed array of numbers', () => {
expect(sum([10, 5, 4, 32, 8])).toBe(59);
});

it('should return the sum when passed array of strings', () => {
expect(sum(['ana', 'marco', 'nicolas', 'tania', 'ptwd'])).toBe(24);
});

it('should return the sum when passed array of mixed strings and numbers - ', () => {
expect(sum([6, 12, 'miami', 1, 'barca', '200', 'lisboa', 8, 10])).toBe(56);
});
it('should return the sum when passed array of mixed strings, numbers and booleans - ', () => {
// false is counted as 0
expect(
sumArray([6, 12, 'miami', 1, 'barca', '200', 'lisboa', 8, false])
).toBe(46);
expect(sum([6, 12, 'miami', 1, 'barca', '200', 'lisboa', 8, false])).toBe(46);
// true is counted as 1
expect(
sumArray([6, 12, 'miami', 1, 'barca', '200', 'lisboa', 8, true])
).toBe(47);
expect(sum([6, 12, 'miami', 1, 'barca', '200', 'lisboa', 8, true])).toBe(47);
});
it('should throw an error when unsupported data type (object or array) present in the array', () => {
// const arr = [6, 12, "miami", 1, "barca", "200", "lisboa", 8, [], {}];

expect(() =>
sumArray([6, 12, 'miami', 1, 'barca', '200', 'lisboa', 8, [], {}])
).toThrow(new Error("Unsupported data type sir or ma'am"));
expect(() => sum([6, 12, 'miami', 1, 'barca', '200', 'lisboa', 8, [], {}])).toThrow(
new Error("Unsupported data type sir or ma'am")
);
});
});

Expand All @@ -112,6 +128,7 @@ describe('Calculate the average of an array of numbers', () => {
});

it('should return null if receives an empty array when called', () => {
// should it return null or zero?
expect(averageNumbers([])).toBe(null);
});

Expand All @@ -136,6 +153,7 @@ describe('Calculate the average of an array of strings', () => {
});

it('should return null if receives an empty array when called', () => {
// should it return null or zero?
expect(averageWordLength([])).toBe(null);
});

Expand All @@ -146,20 +164,29 @@ describe('Calculate the average of an array of strings', () => {

it('should return the average of a the array', () => {
expect(
averageWordLength([
'Ironhack',
'Madrid',
'Barcelona',
'Paris',
'Miami',
'Mexico',
'Berlin',
'Programmers'
])
averageWordLength(['Ironhack', 'Madrid', 'Barcelona', 'Paris', 'Miami', 'Mexico', 'Berlin', 'Programmers'])
).toBe(7);
});
});

describe('Bonus: Calculate the average of a mixed elements array', () => {
it('should create a function named avg', () => {
expect(typeof avg).toBe('function');
});

it('should return null if receives an empty array when called', () => {
// should it return null or zero?
expect(avg([])).toBe(null);
});

it('should return the average of the array', () => {
// false is counted as 0
expect(avg([6, 12, 'miami', 1, 'barca', '200', 'lisboa', 8, false])).toBe(5.11);
// true is counted as 1
expect(avg([6, 12, 'miami', 1, 'barca', '200', 'lisboa', 8, true])).toBe(5.22);
});
});

describe('Unique array', () => {
it('should create a function named uniquifyArray', () => {
expect(typeof uniquifyArray).toBe('function');
Expand All @@ -183,17 +210,7 @@ describe('Unique array', () => {

it('should return the uniquified array', () => {
expect(
uniquifyArray([
'iPhone',
'Samsung',
'Android',
'iOS',
'iPhone',
'Samsung',
'Nokia',
'Blackberry',
'Android'
])
uniquifyArray(['iPhone', 'Samsung', 'Android', 'iOS', 'iPhone', 'Samsung', 'Nokia', 'Blackberry', 'Android'])
).toEqual(['iPhone', 'Samsung', 'Android', 'iOS', 'Nokia', 'Blackberry']);
});
});
Expand Down Expand Up @@ -223,12 +240,7 @@ describe('Find elements', () => {
// });

it('should return true if the word we are looking for is in the array', () => {
expect(
doesWordExist(
['pizza', 'sandwich', 'snack', 'soda', 'book', 'computer'],
'book'
)
).toBe(true);
expect(doesWordExist(['pizza', 'sandwich', 'snack', 'soda', 'book', 'computer'], 'book')).toBe(true);
});
});

Expand All @@ -242,9 +254,7 @@ describe('Count repetition', () => {
});

it('should return 1 (one) when the word appears only one time in the array', () => {
expect(howManyTimes(['basketball', 'football', 'tennis'], 'tennis')).toBe(
1
);
expect(howManyTimes(['basketball', 'football', 'tennis'], 'tennis')).toBe(1);
});

it("should return 0 (zero) when the word doesn't appear in the array", () => {
Expand Down