Skip to content

ZA-ITP-May-2025 | Christian Mayamba | Module-Data-Groups | Week 1 #573

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

Open
wants to merge 5 commits into
base: main
Choose a base branch
from
Open
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
17 changes: 17 additions & 0 deletions Sprint-1/implement/sum.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,21 @@
function sum(elements) {
if (elements.length === 0) {
return 0;
}

if (elements.every((item) => typeof item === "string")) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same questions as above

return "undefined";
}

const numbersArray = elements.filter((item) => typeof item === "number");

let sum1 = 0;
for (let i = 0; i < numbersArray.length; i++) {
sum1 += numbersArray[i];
}
return sum1;
}
testVar = [10, "jesus", 20, "hi"];
console.log(sum(testVar));

module.exports = sum;
19 changes: 18 additions & 1 deletion Sprint-1/implement/sum.test.js
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Tests passed, well done

Original file line number Diff line number Diff line change
Expand Up @@ -13,24 +13,41 @@ const sum = require("./sum.js");
// Given an empty array
// When passed to the sum function
// Then it should return 0
test.todo("given an empty array, returns 0")
test("given an empty array, returns 0", () => {
expect(sum([])).toEqual(0);
});

// Given an array with just one number
// When passed to the sum function
// Then it should return that number
test("given array with one number, returns that number", () => {
expect(sum([10])).toEqual(10);
});

// Given an array containing negative numbers
// When passed to the sum function
// Then it should still return the correct total sum
test("given an array with negative numbers, returns the total sum", () => {
expect(sum([-10, -5, -50])).toEqual(-65);
});

// Given an array with decimal/float numbers
// When passed to the sum function
// Then it should return the correct total sum
test("given an array with decimal numbers, returns the total sum", () => {
expect(sum([10.5, 65.9, 50.8])).toEqual(127.2);
});

// Given an array containing non-number values
// When passed to the sum function
// Then it should ignore the non-numerical values and return the sum of the numerical elements
test("Non-numerical values should be ignored and return the sum of the rest", () => {
expect(sum(["hey", 10, "hi", 60, 10])).toEqual(80);
});

// Given an array with only non-number values
// When passed to the sum function
// Then it should return the least surprising value given how it behaves for all other inputs
test("Only non-number values should return undefined", () => {
expect(sum(["hey", "love", "hi", "hate"])).toEqual("undefined");
});