Skip to content

London | ITP-May-2025 | Sisay Mehari | Module-Data-Groups | Sprint 1 #556

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 9 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
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
19 changes: 16 additions & 3 deletions Sprint-1/fix/median.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,22 @@
// 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)) {
return null;
}
const arrFilter = list.filter (item => typeof item === "number" && !isNaN(item)
);
if (arrFilter.length === 0)
return null;
const sortedArr = arrFilter.sort((a,b) => a - b);
const len = sortedArr.length;
const middleIndex = Math.floor(len / 2);
if (len % 2 !== 0){
return sortedArr[middleIndex];
} else{
return (sortedArr[middleIndex - 1] + sortedArr[middleIndex]) / 2;
}
}


module.exports = calculateMedian;
2 changes: 1 addition & 1 deletion Sprint-1/fix/median.test.js
Copy link

Choose a reason for hiding this comment

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

Tests passed, well done

Original file line number Diff line number Diff line change
Expand Up @@ -47,4 +47,4 @@ describe("calculateMedian", () => {
].forEach(({ input, expected }) =>
it(`filters out non-numeric values and calculates the median for [${input}]`, () => expect(calculateMedian(input)).toEqual(expected))
);
});
});
12 changes: 11 additions & 1 deletion Sprint-1/implement/dedupe.js
Copy link

Choose a reason for hiding this comment

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

tests passed, well done

Original file line number Diff line number Diff line change
@@ -1 +1,11 @@
function dedupe() {}
function dedupe(list) {
const unique = [...new Set(list)];
return unique;
}

// console.log(dedupe(['apple', 'banana', 'apple', 1, 2, 1]));




module.exports = dedupe;
13 changes: 11 additions & 2 deletions Sprint-1/implement/dedupe.test.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
const dedupe = require("./dedupe.js");
const dedupe = require("./dedupe");
/*
Dedupe Array

Expand All @@ -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", () => {
expect(dedupe([4,9,12])).toEqual([4,9,12]);

});

// 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 string or number , it removes duplicate values and preserve the first occurence of the element", ()=> {
expect(dedupe(['apple', 'banana', 'apple', 1, 2, 1])).toEqual(['apple', 'banana', 1, 2]);
});
6 changes: 6 additions & 0 deletions Sprint-1/implement/max.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,10 @@
function findMax(elements) {
const filtered = elements.filter(elements => typeof elements === "number" && !isNaN(elements))
const max = Math.max(...filtered);
return max;
}



console.log(findMax(['apple', 'banana', null, undefined]));
module.exports = findMax;
25 changes: 24 additions & 1 deletion Sprint-1/implement/max.test.js
Copy link

Choose a reason for hiding this comment

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

Tests passed, well done

Original file line number Diff line number Diff line change
Expand Up @@ -16,28 +16,51 @@ 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([])).toEqual(-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([10])).toEqual(10);
})



// 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 overall", () =>{
expect(findMax([1, -2, 3, -4, 5])).toEqual(5);
})

// 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([-1, -2, -3])).toEqual(-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([1.5, 2.3, 0.7])).toEqual(2.3);
});

// 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 ignores non-numeric values", () => {
expect(findMax([3, 'apple', null, 7, NaN, 5])).toEqual(7);
});


// 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(['apple', 'banana', null, undefined])).toEqual(-Infinity);
});
7 changes: 7 additions & 0 deletions Sprint-1/implement/sum.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,11 @@
function sum(elements) {
const filtered = elements.filter(elements => typeof elements === "number" && !isNaN(elements) );
if (filtered.length === 0) {
return 0;
}else {
const sumTotal = filtered.reduce((total, elements) => total + elements, 0);
return sumTotal;
}
}

module.exports = sum;
20 changes: 18 additions & 2 deletions Sprint-1/implement/sum.test.js
Copy link

Choose a reason for hiding this comment

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

Tests passed, well done

Original file line number Diff line number Diff line change
Expand Up @@ -13,24 +13,40 @@ 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([])).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, returns that number", () => {
expect(sum([10])).toEqual(10);
});

// 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, returns the correct total sum", () => {
expect(sum([10, -2, -4])).toEqual(4);
});

// 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([0.23, 0.1, 2.3])).toEqual(2.63);
});

// 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, ignores non-numerical values and returns the sum of numerical elements", () => {
expect(sum([3, 'apple', null, 7, NaN, 5])).toEqual(15);
});
// 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, returns 0", () => {
expect(sum(['apple', 'banana', null, undefined])).toEqual(0);
});
7 changes: 3 additions & 4 deletions Sprint-1/refactor/includes.js
Copy link

Choose a reason for hiding this comment

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

code looks fine

Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
// 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;
for (const item of list) {
if (item === target) {
return true;
}
}
return false;
Expand Down
17 changes: 17 additions & 0 deletions Sprint-1/stretch/aoc-2018-day1/solution.js
Copy link

Choose a reason for hiding this comment

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

consider how you would code to catch errors such as file not being available. just a consideration, w3 schools is a website with good information

Copy link

Choose a reason for hiding this comment

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

This would work, well done

Copy link

Choose a reason for hiding this comment

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

Each type of error can be identified and returned however that should be discussed later in the course. Do your own research in the meantime.

Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
const fs = require('fs');

const filePath = 'input.txt';
try {
const content = fs.readFileSync(filePath, 'utf8');
const frequencyChanges = content
.split('\n')
.filter(line => line.trim() !== '')
.map(str => parseInt(str, 10));


const total = frequencyChanges.reduce((a, b) => a + b, 0);

console.log('Total frequency:', total);
}catch (err) {
console.error('Error reading file:', err);
}
3 changes: 0 additions & 3 deletions Sprint-2/implement/contains.js

This file was deleted.

35 changes: 0 additions & 35 deletions Sprint-2/implement/contains.test.js

This file was deleted.