Skip to content

WM | May-2025 | Abdullah Saleh | Module-Data-Groups | Sprint1 #561

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 22 commits into
base: main
Choose a base branch
from
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
808cd40
Declare array variables.
3bdullah-saleh Jun 28, 2025
a77628d
Iteration throw a function
3bdullah-saleh Jun 28, 2025
365a036
Create a function for finding the mean and add some tests.
3bdullah-saleh Jun 28, 2025
954a7d2
Create a function to sum the values but it is a flawed approach.
3bdullah-saleh Jun 28, 2025
11dc49b
Create a function to find Median of the list
3bdullah-saleh Jun 28, 2025
985755c
Assembling the Median and Mean function.
3bdullah-saleh Jun 28, 2025
29e8832
Fix the implementation of calculateMedian so it passes all tests
3bdullah-saleh Jul 6, 2025
e940258
Write test cases and Implement the function to passed the tests
3bdullah-saleh Jul 7, 2025
d206079
Implement the function to handel the test cases.
3bdullah-saleh Jul 7, 2025
c0dfdc1
Change the last test message to match the expected output.
3bdullah-saleh Jul 7, 2025
811a87d
Implement the function and created test cases.
3bdullah-saleh Jul 7, 2025
fad759b
Solve Day 1 Part 1 - Calculate resulting frequency
3bdullah-saleh Jul 8, 2025
b5aa2c7
filter non numeric value using 'Number.isFinite()' to filter out 'NaN…
3bdullah-saleh Jul 30, 2025
a4d5622
fix the Indentation
3bdullah-saleh Jul 30, 2025
c29e0ab
update the test to check the reference of the returned array is diffe…
3bdullah-saleh Jul 31, 2025
f74b780
refactor the code to handel '-Infinity' cases properly
3bdullah-saleh Jul 31, 2025
e46a7f3
refactor the function to handle the test cases correctly and handling…
3bdullah-saleh Jul 31, 2025
a182606
Refactor sum function to return 0 when no numeric values are present.…
3bdullah-saleh Jul 31, 2025
1ed85d4
remove unnecessary else block after return for cleaner control flow
3bdullah-saleh Jul 31, 2025
3e0ee17
correct assertion order in test to match expected pattern
3bdullah-saleh Jul 31, 2025
d3b97e0
handle empty numeric array by returning -Infinity in findMax function
3bdullah-saleh Jul 31, 2025
b6fc9d6
Change test description to match the test accordingly.
3bdullah-saleh Jul 31, 2025
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
31 changes: 27 additions & 4 deletions Sprint-1/fix/median.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,32 @@
// 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;
}
// Check if the list is an array and not empty
if (!Array.isArray(list) || list.length === 0) {
return null;
}

// Filter out non-numeric values (NaN, undefined, etc.)
const numbersOnly = list.filter((item) => Number.isFinite(item));

// If no numeric values remain after filtering, return null
if (numbersOnly.length === 0) {
return null;
}

// Sort the filtered array in ascending order
numbersOnly.sort((a, b) => a - b);
const middleIndex = Math.floor(numbersOnly.length / 2);

// If odd length, return the middle element
if (numbersOnly.length % 2 !== 0) {
return numbersOnly[middleIndex];
}
// If even length, return average of two middle elements
else {
const middle1 = numbersOnly[middleIndex - 1];
const middle2 = numbersOnly[middleIndex];
return (middle1 + middle2) / 2;
}
}
module.exports = calculateMedian;
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(array) {
let arrayCopy = [];
if (array.length == 0) {
return [];
}
array.forEach((element) => {
if (!arrayCopy.includes(element)) {
arrayCopy.push(element);
}
});
return arrayCopy;
}

module.exports = dedupe;
17 changes: 13 additions & 4 deletions Sprint-1/implement/dedupe.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,21 @@ E.g. dedupe([1, 2, 1]) target output: [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, it returns a copy of the original array", () => {
const input = ["a", "b", "c"];
expect(dedupe(input)).not.toBe(input); // Not the same reference
expect(dedupe(input)).toEqual(input); // But equal in content
});
// Given an array with strings or numbers
// When passed to the dedupe function
// Then it should remove the duplicate values, preserving the first occurence of each element
// Then it should remove the duplicate values, preserving the first occurrence of each element
test("Given an array with strings or numbers, it return an array with no duplicate ", () => {
expect(dedupe(["a", "a", "b", "c", "a", "d"])).toEqual(["a", "b", "c", "d"]);
expect(dedupe([1, 2, 2, 3, 4, 5, 6, 6, 9])).toEqual([1, 2, 3, 4, 5, 6, 9]);
});
14 changes: 13 additions & 1 deletion Sprint-1/implement/max.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,16 @@
/**
* find the largest numerical element of an array.
*/
function findMax(elements) {
}
// Function will return '-Infinity', If the array is empty or has only non numeric value.
// If the array has only one element, function will return that element as the largest value.
// If the array has only negative value, function will return the closest number to zero.
// Otherwise, function will return the largest value.
const numbersOnly = elements.filter((element) => Number.isFinite(element));
if (numbersOnly.length === 0) {
return -Infinity;
}
return Math.max(...numbersOnly);
};

module.exports = findMax;
36 changes: 35 additions & 1 deletion Sprint-1/implement/max.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,28 +16,62 @@ 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, should return -Infinity", () => {
const input = []
const output = -Infinity
expect(findMax(input)).toEqual(output)
});

// 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, should return that number", () => {
const input = [1]
const output = 1
expect(findMax(input)).toEqual(output)
});

// 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, should return the largest number overall", () => {
const input = [1, 2, -1, 5, 10];
const output = 10;
expect(findMax(input)).toEqual(output);
});

// 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, should return the closest number to zero", () => {
const input = [-1, -2, -1, -5, -15];
const output = -1;
expect(findMax(input)).toEqual(output);
});

// 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, should return the largest decimal number", () => {
const input = [1.5, 2.6, 1.567, 5.2, 10.03];
const output = 10.03;
expect(findMax(input)).toEqual(output);
});

// 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-number values, should return the max and ignore non-numeric values", () => {
const input = ['hey', 10, 'hi', 60, 10]
const output = 60
expect(findMax(input)).toEqual(output)
});

// 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, should return Infinity", () => {
const input = ['hey', "10", 'hi', "60", "10"]
const output = -Infinity
expect(findMax(input)).toEqual(output)
});
8 changes: 6 additions & 2 deletions Sprint-1/implement/sum.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,8 @@
function sum(elements) {
}

const numbersOnly = elements.filter((element) => typeof element === "number");
return numbersOnly.reduce(
(accumulator, currentValue) => accumulator + currentValue,
0
);
};
module.exports = sum;
29 changes: 27 additions & 2 deletions Sprint-1/implement/sum.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,30 +7,55 @@ E.g. sum(['hey', 10, 'hi', 60, 10]), target output: 80 (ignore any non-numerical
*/

const sum = require("./sum.js");

// Acceptance Criteria:

// 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, should return 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 an array with just one number, should return that number", () => {
expect(sum([1])).toEqual(1);
});

// 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 containing negative numbers, should still return the correct total sum", () => {
const input = [-1, -5, -3];
const output = -9;
expect(sum(input)).toEqual(output);
});

// 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, should return the correct total sum", () => {
const input = [1.2, 0.6, 0.005];
const output = 1.805;
expect(sum(input)).toBeCloseTo(output);
});

// 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 an array containing non-number values, should return the sum of the numerical elements only", () => {
const input = [1.5, "2.4", 3.5, "hello", 2];
const output = 7;
expect(sum(input)).toEqual(output);
});

// 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, should return 0", () => {
const input = ["2.4", "hello", null, undefined];
const output = 0;
expect(sum(input)).toEqual(output);
});
10 changes: 10 additions & 0 deletions Sprint-1/stretch/aoc-2018-day1/solution.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
const fs = require('fs');

// Read the file synchronously
const data = fs.readFileSync('input.txt', 'utf8').split('\n');
let currentFrequency = 0;
for (let index = 0; index < data.length; index++ ) {
currentFrequency = currentFrequency + Number(data[index]);

}
console.log(currentFrequency);
9 changes: 9 additions & 0 deletions prep/array.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
// declaring array literal as follows

const items = [4.6, 5.9, 6.99];

// In JavaScript, we can use square bracket notation to access specific elements in the array using an index.

items[0]; // evaluates to 4.6
items[1]; // evaluates to 5.9
items[2]; // evaluates to 6.99
11 changes: 11 additions & 0 deletions prep/assembling.the.parts.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
const calculateMedian = require('./median');
const calculateMean = require('./mean');

const salaries = [10, 20, 30, 40, 60, 80, 80];
const median = calculateMedian(salaries);

console.log(salaries, "<--- salaries input before we call calculateMean");
const mean = calculateMean(salaries);

console.log(`The median salary is ${median}`);
console.log(`The mean salary is ${mean}`);
14 changes: 14 additions & 0 deletions prep/iteration.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
// In programming, the process of repeating something is called iteration.
// In programming, we can iterate by using a loop🧶
// 🧶 loop: A loop is a sequence of instructions that is continually repeated until some condition is reached.
// In particular, we can use a for...of statement to sum the elements of the array.

function calculateMean(list) {
let total = 0;

for (const item of list) {
total += item;
}

}
calculateMean([10, 20, 30, 40, 50]);
8 changes: 8 additions & 0 deletions prep/mean.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
function calculateMean(list) {
let total = 0;
for (const item of list) {
return total += item;
}
}

module.exports = calculateMean;
15 changes: 15 additions & 0 deletions prep/mean.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
// Let’s consider a problem where we calculate the mean of a list of numbers.

// Given an array of numbers
// When we call calculateMean with the array of numbers
// Then we get the mean.

const calculateMean = require('./median');

test("calculates the mean of a list of numbers", () => {
const list = [3, 50, 7];
const currentOutput = calculateMean(list);
const targetOutput = 20;

expect(currentOutput).toEqual(targetOutput); // 20 is (3 + 50 + 7) / 3
});
9 changes: 9 additions & 0 deletions prep/median.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
function calculateMedian(list) {
const middleIndex = Math.floor(list.length / 2);
const median = list.splice(middleIndex, 1)[0];

return median;

}

module.exports = calculateMedian;
9 changes: 9 additions & 0 deletions prep/median.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
const calculateMedian = require('./median');

test("calculates the median of a list of odd length", () => {
const list = [10, 20, 30, 50, 60];
const currentOutput = calculateMedian(list);
const targetOutput = 30;

expect(currentOutput).toEqual(targetOutput);
});
16 changes: 16 additions & 0 deletions prep/summation.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
// To sum a list we can start by creating a variable total with an initial value of 0.
// We then need to repeatedly add each value in the list to our total.

function sumValues(list) {
let total = 0;
total += list[0]; // access a list element and add to total
total += list[1];
total += list[2];
total += list[3];
total += list[4];
return total;
}

console.log(sumValues([1, 2, 3, 4, 5]));
// However, this approach is flawed.