Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
36 commits
Select commit Hold shift + click to select a range
c9145df
CalculateMedian function implemented
Jul 16, 2026
1cf01db
Comment updated on return null
Jul 16, 2026
256ef50
Function calculateMedian updated so it could calculate even elements …
Jul 16, 2026
973df91
findMax function implemented to check for the largest number in a giv…
Jul 18, 2026
535f62f
first test case for an empty array input written, tested and passed.
Jul 20, 2026
ff4538c
Dedupe function implemented to return an empty array.
Jul 20, 2026
f713ab1
Array copied and sorted. Array checked for non numeric array
Jul 21, 2026
022321e
Tested for unsorted array and not an array.
Jul 21, 2026
3234e57
If statement for filtering through non numeric value implemented for …
Jul 21, 2026
e60e7e1
Test for filtering out non-numeric values and calculates the median t…
Jul 21, 2026
15d96c8
Test case for non duplicate array created.
Jul 22, 2026
d336151
Test for mixed numbers and strings created.
Jul 22, 2026
f075a6e
Function implemented to check for duplicate elements in a given array.
Jul 22, 2026
3b5f6cd
Brackets removed from the second test inside expect.
Jul 22, 2026
455f1b8
last tested fixed by putting strings inside quotes.
Jul 22, 2026
8351878
console.log for removed
Jul 22, 2026
dccd5c5
Description of the last test updated.
Jul 22, 2026
c31c00a
First test case for checking an array with one number created.
Jul 22, 2026
129f635
A second if statement added for checking non numeric values and retur…
Jul 23, 2026
aa46855
Last test fixed for bug for testing non numeric array and return unde…
Jul 23, 2026
c37845d
Extra brackets removed
Jul 23, 2026
763e7ce
All required test cases created.
Jul 23, 2026
1a697cc
function implemented for checking calculating sum of numeric array
Jul 23, 2026
f86b512
Error sums inside toBe corrected.
Jul 23, 2026
b1c125a
A second if statement added to check for an array with non-numeric an…
Jul 23, 2026
6e687da
Sum calculation errors corrected on the last test case.
Jul 23, 2026
57a3258
Merge branch 'CodeYourFuture:main' into coursework/sprint-1
russom-g Jul 30, 2026
926ec28
Restore untouched file to match main
Jul 30, 2026
035b8f5
Merge branch 'coursework/sprint-1' of https://github.com/russom-g/Mod…
Jul 30, 2026
8901c43
Restore untouched file to match main
Jul 30, 2026
0f31552
Restore untouched files to match main
Jul 30, 2026
a4904af
Tittle changed to alarm clock app
Jul 30, 2026
bcb050c
Remove file not present on main and restore readme
Jul 30, 2026
01f497f
Restore readme to match upstream main
Jul 30, 2026
c8c9656
The for loop changed into for of loop
Jul 30, 2026
1ed419e
test checked and passed
Jul 30, 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
20 changes: 16 additions & 4 deletions Sprint-1/fix/median.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,21 @@
// 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;
}
if (!Array.isArray(list) || list.length === 0) {
return null;
}

const numbers = list.filter((item) => typeof item === "number");
if (numbers.length === 0) {
return null;
}

numbers.sort((a, b) => a - b);
const middleIndex = Math.floor(numbers.length / 2);

if (numbers.length % 2 === 0) {
return (numbers[middleIndex - 1] + numbers[middleIndex]) / 2;
}
return numbers[middleIndex];
}
module.exports = calculateMedian;
30 changes: 20 additions & 10 deletions Sprint-1/fix/median.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,8 @@ describe("calculateMedian", () => {
{ input: [1, 2, 3, 4], expected: 2.5 },
{ input: [1, 2, 3, 4, 5, 6], expected: 3.5 },
].forEach(({ input, expected }) =>
it(`returns the median for [${input}]`, () => expect(calculateMedian(input)).toEqual(expected))
it(`returns the median for [${input}]`, () =>
expect(calculateMedian(input)).toEqual(expected))
);

[
Expand All @@ -24,17 +25,26 @@ describe("calculateMedian", () => {
{ input: [110, 20, 0], expected: 20 },
{ input: [6, -2, 2, 12, 14], expected: 6 },
].forEach(({ input, expected }) =>
it(`returns the correct median for unsorted array [${input}]`, () => expect(calculateMedian(input)).toEqual(expected))
it(`returns the correct median for unsorted array [${input}]`, () =>
expect(calculateMedian(input)).toEqual(expected))
);

it("doesn't modify the input array [3, 1, 2]", () => {
it("doesn't modify the input array [3, 1, 2]", () => {
const list = [3, 1, 2];
calculateMedian(list);
expect(list).toEqual([3, 1, 2]);
});

[ 'not an array', 123, null, undefined, {}, [], ["apple", null, undefined] ].forEach(val =>
it(`returns null for non-numeric array (${val})`, () => expect(calculateMedian(val)).toBe(null))

[
"not an array",
123,
null,
undefined,
{},
[],
["apple", null, undefined],
].forEach((val) =>
it(`returns null for non-numeric array (${val})`, () =>
expect(calculateMedian(val)).toBe(null))
);

[
Expand All @@ -45,6 +55,6 @@ describe("calculateMedian", () => {
{ input: [3, "apple", 1, null, 2, undefined, 4], expected: 2.5 },
{ input: ["banana", 5, 3, "apple", 1, 4, 2], expected: 3 },
].forEach(({ input, expected }) =>
it(`filters out non-numeric values and calculates the median for [${input}]`, () => expect(calculateMedian(input)).toEqual(expected))
);
});
it(`filters out non-numeric values and calculates the median for [${input}]`, () =>
expect(calculateMedian(input)).toEqual(expected))
)});
15 changes: 14 additions & 1 deletion Sprint-1/implement/dedupe.js
Original file line number Diff line number Diff line change
@@ -1 +1,14 @@
function dedupe() {}
function dedupe(arr) {
const elements = [];
for (const item of arr) {
if (!elements.includes(item)) {
elements.push(item);
}
}
if (elements.length === arr.length) {
return arr.slice();
}
return elements;
}

module.exports = dedupe;
19 changes: 17 additions & 2 deletions Sprint-1/implement/dedupe.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,13 +16,28 @@ 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");
test("given an empty array, it returns an empty array", function() {
console.log(dedupe)
expect(dedupe([])).toEqual([]);

});

// Given an array with no duplicates
// When passed to the dedupe function
// Then it should return a copy of the original array
test("given non duplicate array, it returns a copy of the array", function () {
const array = [1, 2, 3];
const result = dedupe(array);
expect(result).toEqual(array);
});

// Given an array of strings or numbers
// When passed to the dedupe function
// 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.
test("given an array of strings or numbers, it returns the original occurrence of the array", function () {
const array = [1, "c", 2, 3, "d"];
const result = dedupe(array);
expect(result).toEqual(array);
});
10 changes: 9 additions & 1 deletion Sprint-1/implement/max.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,12 @@
function findMax(elements) {
let largestNum;
for (let i = 0; i < elements.length; i++) {
if (typeof elements[i] === "number") {
if (largestNum === undefined || elements[i] > largestNum) {
largestNum = elements[i];
}
}
}
return largestNum;
}

module.exports = findMax;
22 changes: 21 additions & 1 deletion Sprint-1/implement/max.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,28 +16,48 @@ 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("given an empty array, returns -Infinity", function() {
expect(findMax([])).toBeUndefined();
});

// Given an array with one number
// When passed to the max function
// Then it should return that number
test("given an array, with one number returns that number", function () {
expect(findMax([5])).toBe(5);
});

// Given an array with both positive and negative numbers
// When passed to the max function
// Then it should return the largest number overall
test("given an array with positive and negative numbers, returns the largest", function () {
expect(findMax([-1, -5, 1, 4])).toBe(4);
});

// Given an array with just negative numbers
// When passed to the max function
// Then it should return the closest one to zero
test("given an array with negative numbers, returns the closest to zero", function () {
expect(findMax([-3, -2, -1])).toBe(-1);
});

// Given an array with decimal numbers
// When passed to the max function
// Then it should return the largest decimal number
test("given an empty array, returns -Infinity", function () {
expect(findMax([6.5, 1.7, 2.3])).toBe(6.5);
});

// 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("given an array with non numeric value, returns the max ignoring the non numeric value", function () {
expect(findMax([-1, -5, 1, 4])).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, returns the least surprising value", function () {
expect(findMax(["a", "b", "c"])).toBe(undefined);
});
16 changes: 16 additions & 0 deletions Sprint-1/implement/sum.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,20 @@
function sum(elements) {
let sum = 0;
let hasNumber = false;

for (let i = 0; i < elements.length; i++) {
if (typeof elements[i] === "number") {
sum += elements[i];
hasNumber = true;
}
}
if (hasNumber === false && elements.length >0) {
return undefined;
}
return sum;
}

let numbers = ["c", "b", "hi", 1];
console.log(sum(numbers));

module.exports = sum;
20 changes: 19 additions & 1 deletion Sprint-1/implement/sum.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,24 +13,42 @@ 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", function() {
expect(sum([])).toBe(0);
});

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

// Given an array containing negative numbers
// When passed to the sum function
// Then it should still return the correct total sum
test("given a negative array, returns the sum", function() {
expect(sum([-4, -2, -1])).toBe(-7);
});

// Given an array with decimal/float numbers
// When passed to the sum function
// Then it should return the correct total sum
test("given a decimal array, returns the sum", function() {
expect(sum([1.9, 3.1, 2.7])).toBe(7.7);
});

// 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("given a mixed array, returns only the sum of numbers ", function() {
expect(sum(["c", 3, 4, "hi", 7])).toBe(14);
});

// 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 of non-numeric value, returns undefined", function() {
expect(sum(["b", "hello", "y"])).toBe(undefined);
});
3 changes: 1 addition & 2 deletions Sprint-1/refactor/includes.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
// Refactor the implementation of includes to use a for...of loop

function includes(list, target) {
for (let index = 0; index < list.length; index++) {
const element = list[index];
for (const element of list) {
if (element === target) {
return true;
}
Expand Down
1 change: 1 addition & 0 deletions Sprint-1/refactor/includes.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -36,3 +36,4 @@ test("searches for null", () => {

expect(currentOutput).toEqual(targetOutput);
});
// test checked and passed.
Loading