Skip to content

Commit d7af84e

Browse files
author
JorvanW
committed
updated tally.js and tally.test to turn and empty array into a empty object as required
1 parent edb970d commit d7af84e

2 files changed

Lines changed: 28 additions & 27 deletions

File tree

‎Sprint-2/implement/tally.js‎

Lines changed: 20 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,30 @@
1-
function tally(array) { //(array) is the input to the function
1+
function tally(array) {
2+
//(array) is the input to the function
23

3-
if (!Array.isArray(array)){ //checking if array is actually an array
4-
throw new Error("Input must be an array");
4+
if (!Array.isArray(array)) {
5+
//checking if array is actually an array
6+
throw new Error("Input must be an array");
57
}
6-
7-
if (array.length === 0){ //if array is empty return a empty array
8-
return []
8+
9+
if (array.length === 0) {
10+
//if array is empty return a empty array
11+
return {};
912
}
10-
11-
12-
const result = {} //creates result as an empty object which will store the counts
13-
1413

15-
for (const item of array){ //'for' loops through every item in the array
16-
if (result[item]){ // checks if the item is already in the result{}
17-
result[item]++} // If it is then '++' tells it to add 1 to the value
18-
14+
const result = {}; //creates result as an empty object which will store the counts
15+
16+
for (const item of array) {
17+
//'for' loops through every item in the array
18+
if (result[item]) {
19+
// checks if the item is already in the result{}
20+
result[item]++;
21+
} // If it is then '++' tells it to add 1 to the value
1922
else {
20-
result[item] = 1; // else is saying that if it doesn't exist then we give it a value of 1
23+
result[item] = 1; // else is saying that if it doesn't exist then we give it a value of 1
2124
}
22-
23-
}
25+
}
2426

25-
return result;
27+
return result;
2628
}
2729

2830
module.exports = tally;

‎Sprint-2/implement/tally.test.js‎

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -19,28 +19,27 @@ const tally = require("./tally.js");
1919
// Given a function called tally
2020
// When passed an array of items
2121
// Then it should return an object containing the count for each unique item
22-
test("tally should return an count of each item passed through an array ",() => {
23-
expect(tally(['a', 'b', 'c'])).toEqual({a: 1, b: 1, c: 1});
22+
test("tally should return an count of each item passed through an array ", () => {
23+
expect(tally(["a", "b", "c"])).toEqual({ a: 1, b: 1, c: 1 });
2424
});
2525

2626
// Given an empty array
2727
// When passed to tally
2828
// Then it should return an empty object
29-
test("tally on an empty array returns an empty object",() => {
30-
expect(tally([])).toEqual([]);
29+
test("tally on an empty array returns an empty object", () => {
30+
expect(tally([])).toEqual({});
3131
});
3232

3333
// Given an array with duplicate items
3434
// When passed to tally
3535
// Then it should return counts for each unique item
36-
test("tally on an array with duplicate items return a count for each item ",() => {
37-
expect(tally(['a', 'a', 'b', 'c'])).toEqual({a: 2, b: 1, c: 1});
36+
test("tally on an array with duplicate items return a count for each item ", () => {
37+
expect(tally(["a", "a", "b", "c"])).toEqual({ a: 2, b: 1, c: 1 });
3838
});
3939

4040
// Given an invalid input like a string
4141
// When passed to tally
4242
// Then it should throw an error
43-
test("tally with a invalid string should throw an error",() => {
44-
expect(() => tally('car'))
45-
.toThrow("Input must be an array");
43+
test("tally with a invalid string should throw an error", () => {
44+
expect(() => tally("car")).toThrow("Input must be an array");
4645
});

0 commit comments

Comments
 (0)