Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: resolve get array element count #810

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
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
12 changes: 12 additions & 0 deletions lib/averageSpeed.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
/**
Calculate average speend with distance and time
@param {number} distance
@param {number} time
@returns {number} average speed
*/

function averageSpeed(distance, time) {
throw new Error("this method is still a skeleton");
}

module.exports = averageSpeed;
30 changes: 28 additions & 2 deletions lib/getArrayElementCount.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,38 @@
Get count for all occurances of given element or array in an array
If b is an array then it will count the occurrence of all elements in the given first array
@param {array} a
@param {*} b
@param {array|number} b
@returns {number} count value of element
*/

function getArrayElementCount(a, b) {
throw new Error("this method is still a skeleton");
//throw new Error("this method is still a skeleton");
// Verify args types
if (!Array.isArray(a)) {
throw new Error("First argument should be an array");
}

if (typeof b !== "number" && !Array.isArray(b)) {
throw new Error("Second argument should be number");
}

a.forEach(element => {
if(typeof element !== "number") {
throw new Error("First argument should be an array");
}
});

if (Array.isArray(b)) {
b.forEach(element => {
if(typeof element !== "number") {
throw new Error("Second argument should be number");
}
});
}
var arr = Array.isArray(b) ? b : [ b ];

return arr.reduce((prev, curr) =>
prev += a.filter(element => element === curr).length, 0);
}

module.exports = getArrayElementCount;
19 changes: 19 additions & 0 deletions test/averageSpeed.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
const { averageSpeed } = require("../lib");

describe("averageSpeed", () => {
it("should return averages", () => {
expect(averageSpeed(100, 1)).toEqual(100);
expect(averageSpeed(220, 2)).toEqual(110);
expect(averageSpeed(287, 3)).toBeCloseTo(95.666);
});

it("should throw error if first value is not a number", () => {
expect(() => averageSpeed("sdsds", 4)).toThrow("First argument should be a number");
expect(() => averageSpeed([1], 4)).toThrow("First argument should be a number");
});

it("should throw error if first value is not a number", () => {
expect(() => averageSpeed(100, false)).toThrow("Second argument should be a number");
expect(() => averageSpeed(100, "dss")).toThrow("Second argument should be a number");
});
});
8 changes: 4 additions & 4 deletions test/getArrayElementCount.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,12 @@ describe("getArrayElementCount", () => {
});

it("should throw error if first arg is not array", () => {
expect(getArrayElementCount("sdsds", 4)).toThrow("First argument should be an array");
expect(getArrayElementCount(452755, 4)).toThrow("First argument should be an array");
expect(() => getArrayElementCount("sdsds", 4)).toThrow("First argument should be an array");
expect(() => getArrayElementCount(452755, 4)).toThrow("First argument should be an array");
});

it("should throw error if second arg is not present or is not a number or array", () => {
expect(getArrayElementCount([5, 7, 4, 6, 5, 4], false)).toThrow("Second argument should be number");
expect(getArrayElementCount([3, 4, 56, 5, 6], "dss")).toThrow("Second argument should be number");
expect(() => getArrayElementCount([5, 7, 4, 6, 5, 4], false)).toThrow("Second argument should be number");
expect(() => getArrayElementCount([3, 4, 56, 5, 6], "dss")).toThrow("Second argument should be number");
});
});