Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
22 commits
Select commit Hold shift + click to select a range
c2e4f8f
fixed the median function upholding with the test requirements
Ebrahim-Moqbel Aug 5, 2026
d473e23
fixed the bug of the function for the last case of the test
Ebrahim-Moqbel Aug 5, 2026
56d8237
committing the max function built from the max.test cases
Ebrahim-Moqbel Aug 6, 2026
66bf485
implemented the dedupe and sum functions form the test cases files
Ebrahim-Moqbel Aug 6, 2026
0b1b4e1
refactored the includes function
Ebrahim-Moqbel Aug 6, 2026
4568185
Merge branch 'main' into coursework-sprint-1
Ebrahim-Moqbel Sep 10, 2026
f6b9bee
refactoring the last two test cases
Ebrahim-Moqbel Sep 10, 2026
60354c4
refactoring the last two cases of the sum file
Ebrahim-Moqbel Sep 10, 2026
15b25f3
refactored the median and max functions to handle errors and throw th…
Ebrahim-Moqbel Sep 11, 2026
94ba304
refactored the sum function to pass the last two cases added of throw…
Ebrahim-Moqbel Sep 11, 2026
8ae4158
Remove package.json file
Ebrahim-Moqbel Sep 14, 2026
752569c
Remove 'type' field from package.json
Ebrahim-Moqbel Sep 14, 2026
a0441e5
Delete Sprint-1/package.json
Ebrahim-Moqbel Sep 14, 2026
e1f1934
reverting the package.json file changes
Ebrahim-Moqbel Sep 14, 2026
12433de
Merge branch 'coursework-sprint-1' of https://github.com/Ebrahim-Moqb…
Ebrahim-Moqbel Sep 14, 2026
d8d686b
reverted the deleted file
Ebrahim-Moqbel Sep 14, 2026
f490d8b
Rename package and update description for week-1
Ebrahim-Moqbel Sep 14, 2026
2745d87
updating the package.json file
Ebrahim-Moqbel Sep 14, 2026
f8a1096
Merge branch 'coursework-sprint-1' of https://github.com/Ebrahim-Moqb…
Ebrahim-Moqbel Sep 14, 2026
f6ece0a
Remove package.json file
Ebrahim-Moqbel Sep 14, 2026
5a0b2d0
deleting a package.json empty file
Ebrahim-Moqbel Sep 14, 2026
f09df81
Merge branch 'coursework-sprint-1' of https://github.com/Ebrahim-Moqb…
Ebrahim-Moqbel Sep 14, 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
26 changes: 23 additions & 3 deletions Sprint-1/fix/median.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,29 @@
// or contains values that aren't numbers (the function is expected to throw - see the tests).

function calculateMedian(list) {
const middleIndex = Math.floor(list.length / 2);
const median = list.splice(middleIndex, 1)[0];
return median;
// validate the array
if (!Array.isArray(list)) {
throw new Error("calculateMedian requires an array of numbers");
}
// Empty array should throw an error
if (list.length === 0) {
throw new Error("calculateMedian requires a non-empty array");
}

const allNumbers = list.every(
(item) => typeof item === "number" && !Number.isNaN(item)
);
if (!allNumbers) {
throw new Error("calculateMedian requires an array of numbers");
}
const sorted = [...list].sort((a, b) => a - b);
const middleIndex = Math.floor(sorted.length / 2);

if (sorted.length % 2 == 0) {
return (sorted[middleIndex - 1] + sorted[middleIndex]) / 2;
} else {
return sorted[middleIndex];
}
}

module.exports = calculateMedian;
12 changes: 3 additions & 9 deletions Sprint-1/fix/median.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -47,20 +47,14 @@ describe("calculateMedian", () => {
expect(calculateMedian([6, -2, 2, 12, 14])).toEqual(6);
});

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

// There is no median of an empty array, so calculateMedian should throw
// There is no median of an empty array, so calculateMedian should throw an error
it("throws when given an empty array", () => {
expect(() => calculateMedian([])).toThrow(
new Error("calculateMedian requires a non-empty array")
);
});

// Input that isn't an array should throw
// Input that isn't an array should throw an error
it("throws when given a string", () => {
expect(() => calculateMedian("banana")).toThrow(
new Error("calculateMedian requires an array of numbers")
Expand Down Expand Up @@ -91,7 +85,7 @@ describe("calculateMedian", () => {
);
});

// Arrays containing any non-number value should throw, rather than filtering them out
// Arrays containing any non-number value should throw an error , rather than filtering them out
it("throws for an array of strings", () => {
expect(() => calculateMedian(["ten", "twenty", "thirty"])).toThrow(
new Error("calculateMedian requires an array of numbers")
Expand Down
13 changes: 12 additions & 1 deletion Sprint-1/implement/dedupe.js
Original file line number Diff line number Diff line change
@@ -1 +1,12 @@
function dedupe() {}
function dedupe(arr) {
// initialize an empty array
let dedupedArr =[];
for( element of arr){
if(!dedupedArr.includes(element)){
dedupedArr.push(element);
}

}
return dedupedArr
}
module.exports = dedupe;
12 changes: 11 additions & 1 deletion Sprint-1/implement/dedupe.test.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
const dedupe = require("./dedupe.js");

/*
Dedupe Array

Expand All @@ -16,13 +17,22 @@ 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("given an empty array, it returns an empty array", ()=>{
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 an array with no duplicates, returns a copy of the original array", ()=>{
expect(dedupe([3,4,6])).toEqual([3,4,6])

})
// 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.
test('Given an array of strings or numbers, returns a new array with duplicates removed while preserving the first occurrence of each element from the original array',()=>{
expect(dedupe(['h',4,'k','h',3,5,4])).toEqual(['h',4,'k',3,5])
}
)
18 changes: 18 additions & 0 deletions Sprint-1/implement/max.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,22 @@
function findMax(elements) {
// checking if the input is an array
if (!Array.isArray(elements)) {
throw new Error("findMax requires an array of numbers");
}
if (elements.length === 0) {
return -Infinity;
}
const allNumbers = elements.every(
(item) => typeof item === "number" && !Number.isNaN(item)
);
if (!allNumbers) {
throw new Error("findMax requires an array of numbers");
}

const sorted = [...elements].sort((a, b) => a - b);
const max = sorted[sorted.length - 1];

return max;
}

module.exports = findMax;
28 changes: 25 additions & 3 deletions Sprint-1/implement/max.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,28 +16,50 @@ 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", () => {
expect(findMax([])).toBe(-Infinity);
});

// 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", () => {
expect(findMax([22])).toBe(22);
});

// 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 both positive and negative numbers, returns the largest number ", () => {
expect(findMax([-3, 6, -9])).toBe(6);
});
// 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 just negative numbers, returns the closest one to zero", () => {
expect(findMax([-3, -4, -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 array with decimal numbers, returns the largest decimal number", () => {
expect(findMax([3.5, 4.5, 6.5])).toBe(6.5);
});

// Given an array containing a value that isn't a number
// When passed to the max function
// Then it should throw Error("findMax requires an array of numbers")
test("throws an error when given a non number value", () => {
expect(() => findMax(["3", 5, "undefined", null])).toThrow(
new Error("findMax requires an array of numbers")
);
});

// Given something that isn't an array at all, such as "hey", 42 or no argument
// When passed to the max function
// Then it should throw Error("findMax requires an array of numbers")
test("throws an error when given an array with only non-number values", () => {
expect(() => findMax(["Holla!", NaN, true])).toThrow(
new Error("findMax requires an array of numbers")
);
});
20 changes: 20 additions & 0 deletions Sprint-1/implement/sum.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,24 @@
function sum(elements) {
if (!Array.isArray(elements)) {
throw new Error("sum requires an array of numbers");
}

if (elements.length === 0) {
return 0;
}
// every element must be a valid number
const allNumbers = elements.every(
(item) => typeof item === "number" && !Number.isNaN(item)
);
if (!allNumbers) {
throw new Error("sum requires an array of numbers");
}

let total = 0;
for (let i = 0; i < elements.length; i++) {
total += elements[i];
}
return total;
}

module.exports = sum;
24 changes: 22 additions & 2 deletions Sprint-1/implement/sum.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,24 +13,44 @@ 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([])).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, return that number", () => {
expect(sum([3])).toBe(3);
});
// 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, return the correct total", () => {
expect(sum([-2, -4, -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 an array with decimal/float numbers, returns the correct total sum", () => {
expect(sum([3.5, 6.1])).toBe(9.6);
});

// Given an array containing a value that isn't a number
// When passed to the sum function
// Then it should throw Error("sum requires an array of numbers")
test("throws an error when given an array contains a non-number value", () => {
expect(() => sum(["4", "Holla", 6, 9, 1])).toThrow(
new Error("sum requires an array of numbers")
);
});

// Given something that isn't an array at all, such as "hey", 42 or no argument
// When passed to the sum function
// Then it should throw Error("sum requires an array of numbers")
test("throws an error when given a non array value", () => {
expect(() => sum([null, undefined, NaN, true])).toThrow(
new Error("sum requires an array of numbers")
);
});
20 changes: 14 additions & 6 deletions Sprint-1/refactor/includes.js
Original file line number Diff line number Diff line change
@@ -1,13 +1,21 @@
// 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];
if (element === target) {
return true;
// function includes(list, target) {
// for (let index = 0; index < list.length; index++) {
// const element = list[index];
// if (element === target) {
// return true;
// }
// }
// return false;
// }
function includes(list, target){
for (element of list){
if(element === target){
return true
}
}
return false;
return false
}

module.exports = includes;
Loading