Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
34 commits
Select commit Hold shift + click to select a range
0941db3
installed jest as a dev dependency and fix other issue with npm inst…
Tobias-Amaechina Jul 22, 2026
c61d25f
remove splice method to avoid mutating the arrays
Tobias-Amaechina Jul 22, 2026
d2aad92
implement ststement to validate that input data are arrays
Tobias-Amaechina Jul 22, 2026
e01bb4e
implement a statement to convert numerical strings to numbers and ign…
Tobias-Amaechina Jul 22, 2026
10f38f1
validates that the dataset contains numbers
Tobias-Amaechina Jul 22, 2026
1a961a0
implements method to sort the numeric numbers in ascending order
Tobias-Amaechina Jul 22, 2026
cd7c633
update the function to return the median
Tobias-Amaechina Jul 22, 2026
01b6d84
add logic to handle when data length is an odd number
Tobias-Amaechina Jul 22, 2026
33f6952
refactor the code
Tobias-Amaechina Jul 22, 2026
4f2de61
add statement to handle when the data length is even number
Tobias-Amaechina Jul 22, 2026
452383b
Wrote a test case to return an empty array
Tobias-Amaechina Jul 22, 2026
7a97ecd
implement a function to pass the test that returns empty array
Tobias-Amaechina Jul 22, 2026
ab4fe3b
wrote a test that returns original copy of the arrays when there is…
Tobias-Amaechina Jul 22, 2026
0f7c2a1
wrote a test case that retuns no duplice of strings and numbers
Tobias-Amaechina Jul 22, 2026
2377aa1
Wrote a test case to check when empty array is passed to the functio…
Tobias-Amaechina Jul 23, 2026
fd4d588
Implements function to pass the test
Tobias-Amaechina Jul 23, 2026
fea2db7
Wrote a test case to check for when an array has just one number tha…
Tobias-Amaechina Jul 23, 2026
8d6212f
wrote a test case to check for when an array of both +ve and _ve numb…
Tobias-Amaechina Jul 23, 2026
c8b1bf2
Wrote a test case to check and return from an array of -ve numbers th…
Tobias-Amaechina Jul 23, 2026
9a2a914
Wrote a test case 5 to check and return the largest decimal number fr…
Tobias-Amaechina Jul 23, 2026
e6c2e45
wrote a test case to check and return largest numeric value while i…
Tobias-Amaechina Jul 23, 2026
9681bc6
implement the findMax function to filter non-numeric data
Tobias-Amaechina Jul 23, 2026
91f9d57
wrote a test to check for when an array has only non-number value th…
Tobias-Amaechina Jul 23, 2026
b8a5335
Wrote a test case to return zero when the sum function is given an e…
Tobias-Amaechina Jul 23, 2026
4243161
Implements a function to pass the test and returns zero when and emp…
Tobias-Amaechina Jul 23, 2026
eca0d9b
Wrote a test case to return the same number when an array of just on…
Tobias-Amaechina Jul 23, 2026
22b39c4
Wrote a test Case to return the same number when an array of one numb…
Tobias-Amaechina Jul 23, 2026
11549de
Wrote a test case to return the correct total when an array of negati…
Tobias-Amaechina Jul 23, 2026
deec285
Wrote a test case to return correct sum when decimal/float numbers …
Tobias-Amaechina Jul 23, 2026
64af990
add .toFixed method and Number method to handle floating point numebers
Tobias-Amaechina Jul 23, 2026
42d1606
Wrote a test to check for and return the sum of numerical value and …
Tobias-Amaechina Jul 23, 2026
c2dd7ba
updated the function Implementation to pass the test of when non- n…
Tobias-Amaechina Jul 23, 2026
2e66800
Wrote test to check and return the least surprising value when an ar…
Tobias-Amaechina Jul 23, 2026
4d15a8d
Refactored the Implementing function includes to still check the test…
Tobias-Amaechina Jul 23, 2026
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
34 changes: 31 additions & 3 deletions Sprint-1/fix/median.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,37 @@
// or 'list' has mixed values (the function is expected to sort only numbers).

function calculateMedian(list) {
const middleIndex = Math.floor(list.length / 2);
const median = list.splice(middleIndex, 1)[0];
return median;
// validate that the datetype is arrays
if (!Array.isArray(list)) {
return null;
}
// Convert numeric strings to numbers and ignore non-numeric values
const numericValue = list
.map(item=>{
// convert numerical string to number
if(typeof item ==="string" && item.trim()!==""&& !isNaN(item)){
return Number(item);
}
return item;
} )
// Filter every arrays that is now a number
.filter(item =>typeof item==="number"&& !isNaN(item));
// Validates if the Arrays has any numerical values at all
if (numericValue.length===0){
return null;
}
// sorting the numeric data in ascending order
numericValue.sort((a,b)=>a-b);
const median = Math.floor(numericValue.length / 2);

//Add statement to handle when array length is an odd number
if (numericValue.length % 2 !==0){
return numericValue[median];
}
// add a logic to handle when the data length is even number
if (numericValue.length % 2 === 0){
return (numericValue[median-1] + numericValue[median])/2
}
}

module.exports = calculateMedian;
9 changes: 8 additions & 1 deletion Sprint-1/implement/dedupe.js
Original file line number Diff line number Diff line change
@@ -1 +1,8 @@
function dedupe() {}
function dedupe() {
const unique = [...new Set(n)];
return unique;
}



module.exports = dedupe;
32 changes: 31 additions & 1 deletion Sprint-1/implement/dedupe.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,13 +16,43 @@ E.g. dedupe([1, 2, 1]) returns [1, 2]
// Given an empty array
// When passed to the dedupe function
// Then it should return an empty array
test.todo("given an empty array, it returns an empty array");
//test.todo("given an empty array, it returns an empty array");
const dedupe = require("./dedupe.js");

describe("dedupe given an empty array, it returns an empty array ", () => {
[{ input: [], expected: [] }].forEach(({ input, expected }) =>
it(`returns the empty array for [${input}]`, () =>
expect(dedupe(input)).toEqual(expected))
);
});

// Given an array with no duplicates
// When passed to the dedupe function
// Then it should return a copy of the original array

describe("dedupe given an array with no duplicate, it returns a copy of original array ", () => {
[{ input: [1, 2, 3], expected: [1, 2, 3] }].forEach(({ input, expected }) =>
it(`returns the original copy of the array for [${input}]`, () =>
expect(dedupe(input)).toEqual(expected))
);
});



// Given an array of strings or numbers
// When passed to the dedupe function
// Then it should return a new array with duplicates removed while preserving the
// first occurrence of each element from the original array.

describe("dedupe given an array of strings or Numbers , it returns a copy with duplicates in original array removed ", () => {
[
{
input: ["s", "t", "r", "i", "n", "g", "s"],
expected: ["s", "t", "r", "i", "n", "g"],
},
{ input: [1, 1, 2, 3, 3, 4], expected: [1,2,3,4] },
].forEach(({ input, expected }) =>
it(`returns the original copy of the array for [${input}]`, () =>
expect(dedupe(input)).toEqual(expected))
);
});
8 changes: 8 additions & 0 deletions Sprint-1/implement/max.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,12 @@
function findMax(elements) {
// filter out the numeric numbers
const numbers = elements.filter(
(item) => typeof item === "number" && !Number.isNaN(item)
);

const maxiNum = Math.max(...numbers);
return maxiNum;

}

module.exports = findMax;
23 changes: 22 additions & 1 deletion Sprint-1/implement/max.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,28 +16,49 @@ const findMax = require("./max.js");
// When passed to the max function
// Then it should return -Infinity
// Delete this test.todo and replace it with a test.
test.todo("given an empty array, returns -Infinity");
test("case 1: Given an empty array when passed to a function should return -Infinity ", () => {
expect(findMax([])).toBe(-Infinity );
});

// Given an array with one number
// When passed to the max function
// Then it should return that number
test("case 2: Given an array with one number when passed to a function should return that number ", () => {
expect(findMax([6])).toBe(6);
});

// Given an array with both positive and negative numbers
// When passed to the max function
// Then it should return the largest number overall
test("case 3: Given an array with both positive and negative numbers when passed to a function should return the largest number overall ", () => {
expect(findMax([-1,-2,-6,6,8,10])).toBe(10);
});

// Given an array with just negative numbers
// When passed to the max function
// Then it should return the closest one to zero
test("case 4: Given an array with just negative numbers when passed to a function should return the closest number to zero", () => {
expect(findMax([-1, -2, -6, -10, -8, -12])).toBe(-1);
});

// Given an array with decimal numbers
// When passed to the max function
// Then it should return the largest decimal number
test("case 5: Given an array with decimal numbers when passed to a function should return largest decimal number", () => {
expect(findMax([1.1, 0.2, 0.3, 0.4, 0.5, 0.6,2.75])).toBe(2.75);
});

// Given an array with non-number values
// When passed to the max function
// Then it should return the max and ignore non-numeric values
test("case 6: Given an array with numeric and non-numeric values it returns the max and ignores non-numeric values", () => {
expect(findMax([1.1, 3, 4, "Toby", "undefine", "", 2.75])).toBe(4);
});


// Given an array with only non-number values
// When passed to the max function
// Then it should return the least surprising value given how it behaves for all other inputs
test("Given an array with only non-number values, it returns -Infinity", () => {
expect(findMax(["Toby", "Sam", null, undefined, ""])).toBe(-Infinity);
});
4 changes: 4 additions & 0 deletions Sprint-1/implement/sum.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,8 @@
function sum(elements) {
const onlyNumbers = elements.filter(elem=>typeof elem==="number"&&!Number.isNaN(elem));
const addNum = onlyNumbers.reduce((item,n)=>item +n,0);
return Number(addNum.toFixed(10));
}


module.exports = sum;
29 changes: 28 additions & 1 deletion Sprint-1/implement/sum.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,24 +13,51 @@ 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,when passed to the sun function it returns 0",()=>
{expect(sum([])).toBe(0);

});

// Given an array with just one number
// When passed to the sum function
// Then it should return that number
test("Test case 2: given an array with just one number ,when passed to the sun function it returns that number", () => {
expect(sum([5])).toBe(5);
});

// Given an array containing negative numbers
// When passed to the sum function
// Then it should still return the correct total sum
test("Test case 3: given an array containing negative number ,when passed to the sun function it returns the correct total sum ", () => {
expect(sum([-5,-3,-8])).toBe(-16);
});

// Given an array with decimal/float numbers
// When passed to the sum function
// Then it should return the correct total sum
describe("Test case 4: given an array containing decimal/float numbers ,when passed to the sun function it returns the correct total sum ", () => {
[
{ input: [1.1, 2.2, 3.3], expected: 6.6 },
{ input: [-1.1, -2.2, -3.3], expected: -6.6 },
].forEach(({ input, expected }) => it(`returns the correct total sum for [${input}]`,()=>{
expect(sum(input)).toBeCloseTo(expected);

}));

});


// 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("Test case 5: given an array containing both numeric and non-numeric values ,when passed to the sun function it returns the sum of the numeric elements and ignore the non-numerical value ", () => {
expect(sum(["Toby", 6, "undefine","NaN",10, 11])).toBe(27);
});

// 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("given an array with only non-number values,when passed to the sun function it returns 0 which is the least surprising value given how it behaves for all other inputs", () => {
expect(sum(["Toby", "undefine", "NaN"])).toBe(0);
});
Loading
Loading