Skip to content

Sheffield | 25-ITP-May | Declan Williams | Data-Groups-Sprint 1 #695

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 35 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
35 commits
Select commit Hold shift + click to select a range
5decc55
Refactor calculateMedian function to ensure input is an array before …
CatchingKiyoko Jul 4, 2025
5b12f55
Fix calculateMedian function to filter out non-number values from the…
CatchingKiyoko Jul 4, 2025
1c395d6
Fix calculateMedian function to correctly compute the median value fr…
CatchingKiyoko Jul 4, 2025
11a010f
Add check to return null if no valid numbers are present in calculate…
CatchingKiyoko Jul 4, 2025
589fef7
Fix calculateMedian function to ensure sorting occurs before finding …
CatchingKiyoko Jul 4, 2025
fc0d9bc
Refactor includes function to use for...of loop for improved readability
CatchingKiyoko Jul 4, 2025
4199529
implement: initialize newArray ready to store unique values for dedup…
CatchingKiyoko Jul 4, 2025
df0b19c
feat: implement for...of loop to iterate through input array
CatchingKiyoko Jul 4, 2025
a93c361
added check to prevent duplicate values using .includes()
CatchingKiyoko Jul 4, 2025
88ef053
implemented: push non-duplicate items to unique array and return the …
CatchingKiyoko Jul 4, 2025
00abcc4
fix: add missing array parameter to dedupe function
CatchingKiyoko Jul 4, 2025
1e3002d
refactored: removed console.log
CatchingKiyoko Jul 4, 2025
d5cc4cf
reimplmented the module.exports for tests
CatchingKiyoko Jul 4, 2025
5fde149
implemented tests for giving the function an array with no duplicates…
CatchingKiyoko Jul 4, 2025
b07f167
add unit tests for dedupe function to ensure it removes duplicates wh…
CatchingKiyoko Jul 5, 2025
128a345
feat: implement findMax function to filter numbers from an array
CatchingKiyoko Jul 9, 2025
05d5b01
implemented findMax function to return the maximum number from an array
CatchingKiyoko Jul 9, 2025
f630baa
implemented findMax function to return -Infinity when array is empty
CatchingKiyoko Jul 9, 2025
2235414
fix: move empty array check to the correct position in findMax function
CatchingKiyoko Jul 9, 2025
4bcc171
updated test for findMax function when given an empty array
CatchingKiyoko Jul 9, 2025
f84d2f2
added test a array has one number in and returns that number
CatchingKiyoko Jul 9, 2025
e78614b
added jest Test given an array with both positive and negative number…
CatchingKiyoko Jul 9, 2025
ff06a57
added jest Test: given an array with just negative numbers, returns t…
CatchingKiyoko Jul 9, 2025
f0d4e09
added jest Test: given an array with decimal numbers, returns the lar…
CatchingKiyoko Jul 9, 2025
426d205
added jest test: given an array with non-number values, returns the m…
CatchingKiyoko Jul 9, 2025
4221f01
added jest test: given an array with only non-number values, returns …
CatchingKiyoko Jul 9, 2025
b656247
Add check to return 0 if input array is empty in sum function
CatchingKiyoko Jul 12, 2025
5e62085
added jest test for if given an array is empty
CatchingKiyoko Jul 12, 2025
de951df
added for...of loop for to check elements in the arrays are numbers a…
CatchingKiyoko Jul 12, 2025
d87cea0
added jest test for if an array has just one number
CatchingKiyoko Jul 12, 2025
0ab4dfe
added jest tests for if an array contains negative numbers
CatchingKiyoko Jul 12, 2025
93778e0
added jest test for when an array has decima/float numbers
CatchingKiyoko Jul 12, 2025
cabb9a6
added jest test for if an array contains non-numerical values
CatchingKiyoko Jul 12, 2025
da4dd6c
added jest test for when an array has only non-numerical values
CatchingKiyoko Jul 12, 2025
f9e39f9
Merge branch 'CodeYourFuture:main' into Data-Groups-sprint-1
CatchingKiyoko Jul 24, 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
28 changes: 24 additions & 4 deletions Sprint-1/fix/median.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,29 @@
// or 'list' has mixed values (the function is expected to sort only numbers).

function calculateMedian(list) {
// checks if "list" is an array if not return's null.
if(!Array.isArray(list)) return null;

// filter the non number values out of an array like "strings", "null's and undefined"
const numbers = list.filter( value => typeof value === `number` && !isNaN(value));
Copy link
Contributor

Choose a reason for hiding this comment

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

Infinity is also a value of type "number".
If you want to also reject such numbers, you can use Number.isFinite().


// if no numbers are left return null
if (numbers.length === 0) return null;

// sort numbers into ascending order
numbers.sort(function(a, b){
return a - b;
})

// finds the middle index based on filtered numbers length
const middleIndex = Math.floor(list.length / 2);
const median = list.splice(middleIndex, 1)[0];
return median;
}

module.exports = calculateMedian;
// return the correct median based on if its even or odd
if (numbers.length % 2 === 0){
// even length average of two middle numbers
return (numbers[middleIndex - 1] + numbers[middleIndex]) / 2;
} else {
// odd length returns the middle number
return numbers[middleIndex];
}
}
18 changes: 17 additions & 1 deletion Sprint-1/implement/dedupe.js
Original file line number Diff line number Diff line change
@@ -1 +1,17 @@
function dedupe() {}
function dedupe(array) {
// makes a new array to store non duplicate values
const newArray = [];

// loops over each item in the original array
for (const item of array) {
// checks if the values are not in the "newArray"
if (!newArray.includes(item)) {
// if item is not in "newArray" adds it to the array
newArray.push(item);
}
}
// returns the new deduped array
return newArray;
}

module.exports = dedupe;
14 changes: 12 additions & 2 deletions Sprint-1/implement/dedupe.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,22 @@ 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", () => {
expect(dedupe([1, 2, 3])).toEqual([1, 2, 3]);
expect(dedupe(['a', 'b', 'c'])).toEqual(['a', 'b', 'c']);
});
Comment on lines +25 to +28
Copy link
Contributor

Choose a reason for hiding this comment

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

The current test code cannot check if the returned array is a copy of the original array because toEqual() compares objects (including arrays) by value. To illustrate,

  const A = [2, 3, 1];
  const B = [...A];          // B is a copy of A
    
  // This set of code cannot distinguish if the compared objects are the same objects.
  expect(A).toEqual(A);  // true
  expect(A).toEqual(B);  // true

In order to check if the returned array is a copy of the original array, we would need additional checks.
Can you find out what code you need to add in order to ensure the returned value is not the original array?


// 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
test("given an array with duplicates it removes any duplicates and preserves the first instance of each element", () => {
expect(dedupe(['a','a','a','b','b','c'])).toEqual(['a','b','c']);
expect(dedupe([5, 1, 1, 2, 3, 2, 5, 8])).toEqual([5, 1, 2, 3, 8]);
expect(dedupe([1, 2, 1])).toEqual([1, 2]);
});
31 changes: 30 additions & 1 deletion Sprint-1/implement/max.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,33 @@
function findMax(elements) {
}
// makes an array to hold only numbers
const numbersOnly = []

// to loop through each element in the array
for (const item of elements){

// if the item is a number push it to the numbersOnly array
if (typeof item === `number`){
numbersOnly.push(item);
}
}
// start with the first number in the "numbersOnly" array
let max = numbersOnly[0];

// if the no numbers are found in the array returns -infinty
if (numbersOnly.length === 0) {
return -Infinity;
}
Comment on lines +17 to +19
Copy link
Contributor

Choose a reason for hiding this comment

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

It is a better practice to perform length check before line 14. In some other programming languages, line 14 could have caused a runtime error when the array has no elements.


// loop for the largest number in the "numbersOnly" array
for (const num of numbersOnly) {
if (num > max) {
max = num;
}
}
return max;

}
console.log(findMax([30, 50, 10, 40])); // 50
console.log(findMax(['hey', 10, 'hi', 60, 10])); // 60
console.log(findMax([])); // -infinity
module.exports = findMax;
23 changes: 21 additions & 2 deletions Sprint-1/implement/max.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,28 +16,47 @@ 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 one number", () => {
expect(findMax([42])).toBe(42);
});

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

// 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([-100, -10, -5, -50])).toBe(-5);
});

// 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", () => {
expect(findMax([1.5, 2.3, 0.7, ,3.14, 2.71])).toBe(3.14);
});

// 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, returns the max and removes the non-numeric values", () => {
expect(findMax([`hey`, 10, `hi`, 60, 10])).toBe(60);
})

// 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 -Infinity", () => {
expect(findMax([`hey`, `hi`, `hello`])).toBe(-Infinity);
})
13 changes: 13 additions & 0 deletions Sprint-1/implement/sum.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,17 @@
function sum(elements) {
if (elements.length === 0) return 0; // if array is input is empty returns 0

let total = 0; // create a variable to hold the sum

// iterate over each element in the array
for (const value of elements){
// checks if each of the values are numbers
if (typeof value === `number`){
// adds the numbers to the total sum
total += value;
}
}
return total; // returns the total sum after the loop has finished adding all the numbers
Copy link
Contributor

Choose a reason for hiding this comment

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

Indentation of the code at lines 2 and 14 is a bit off.

Have you tried the Prettier VSCode extention?

}

module.exports = sum;
22 changes: 20 additions & 2 deletions 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", () => {
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 a single number return that number", () => {
expect(sum([42])).toBe(42);
})

// 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 correct total sum", () => {
expect(sum([-10, -20, -30])).toBe(-60);
expect(sum([-10, 20, -30])).toBe(-20);
});
// 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 in returns the correct total sum", () => {
expect(sum([10.5, 20.2, 30.3])).toBe(61.0);
expect(sum([10.5, -20.2, 30.3])).toBe(20.6);
});
Comment on lines +37 to +40
Copy link
Contributor

Choose a reason for hiding this comment

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

Decimal numbers in most programming languages (including JS) are internally represented in "floating point number" format. Floating point arithmetic is not exact. For example, the result of 46.5678 - 46 === 0.5678 is false because 46.5678 - 46 only yield a value that is very close to 0.5678. Even changing the order in which the program add/subtract numbers can yield different values.

So the following could happen

  expect( 1.2 + 0.6 + 0.005 ).toEqual( 1.805 );                // This fail
  expect( 1.2 + 0.6 + 0.005 ).toEqual( 1.8049999999999997 );   // This pass
  expect( 0.005 + 0.6 + 1.2 ).toEqual( 1.8049999999999997 );   // This fail

  console.log(1.2 + 0.6 + 0.005 == 1.805);  // false
  console.log(1.2 + 0.6 + 0.005 == 0.005 + 0.6 + 1.2); // false

Can you find a more appropriate way to test a value (that involves decimal number calculations) for equality?

Suggestion: Look up

  • Checking equality in floating point arithmetic in JavaScript
  • Checking equality in floating point arithmetic with Jest


// 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-numerical values ignore non-numerical and returns correct total sum", () => {
expect(sum([2, "hello", 26.5, "hey", -6.6, 8])).toBe(29.9);
});

// 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-numerical values returns 0", () => {
expect(sum(["a", "b", "c"])).toBe(0);
});
7 changes: 5 additions & 2 deletions Sprint-1/refactor/includes.js
Original file line number Diff line number Diff line change
@@ -1,12 +1,15 @@
// 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];
// iterate through each element in the list
for (const element of list){

// If an element matches the target, return true
if (element === target) {
return true;
}
}
// If no element matches the target, return false
return false;
}

Expand Down