Skip to content

London | ITP-May-2025 | Jamal Laqdiem| Module-Data-Groups | Sprint 1 #557

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 15 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
37 changes: 34 additions & 3 deletions Sprint-1/fix/median.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,40 @@
// 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 onlyNumbers = list.filter(item => typeof item === "number" && !isNaN(item))
if (onlyNumbers.length === 0) {
return null
}
const sortedList = [...onlyNumbers].sort((a,b) => a - b)
const middleIndex = Math.floor(sortedList.length / 2);
if (sortedList.length % 2 === 0) {
const median1 = sortedList[middleIndex - 1]
const median2 = sortedList[middleIndex]
return (median1 + median2)/2
} else {
const median = sortedList[middleIndex]
return median
}
}


console.log(calculateMedian([1, 2, 3]))
console.log(calculateMedian([1, 2, 3, 4, 5]))
console.log(calculateMedian([1, 2, 3,4]))
console.log(calculateMedian([3, 1, 2]))
console.log(calculateMedian([5, 1, 3, 4, 2]))
console.log(calculateMedian([4, 2, 1, 3]))
console.log(calculateMedian([6, 1, 5, 3, 2, 4]))
console.log(calculateMedian([1, 2, "3", null, undefined, 4]))
console.log(calculateMedian(["apple", 1, 2, 3, "banana", 4]))
console.log(calculateMedian([1, "2", 3, "4", 5]))
console.log(calculateMedian([1, "apple", 2, null, 3, undefined, 4]))
console.log(calculateMedian([3, "apple", 1, null, 2, undefined, 4]))
console.log(calculateMedian(["banana", 5, 3, "apple", 1, 4, 2]))


module.exports = calculateMedian;

16 changes: 6 additions & 10 deletions Sprint-1/fix/median.test.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,4 @@
// median.test.js

// Someone has implemented calculateMedian but it isn't
// passing all the tests...
// Fix the implementation of calculateMedian so it passes all tests

const calculateMedian = require("./median.js");

describe("calculateMedian", () => {
Expand All @@ -21,16 +16,16 @@ describe("calculateMedian", () => {
{ input: [5, 1, 3, 4, 2], expected: 3 },
{ input: [4, 2, 1, 3], expected: 2.5 },
{ input: [6, 1, 5, 3, 2, 4], expected: 3.5 },
{ 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("doesn't modify the input array [3, 1, 2]", () => {
const list = [3, 1, 2];


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

[ 'not an array', 123, null, undefined, {}, [], ["apple", null, undefined] ].forEach(val =>
Expand All @@ -48,3 +43,4 @@ describe("calculateMedian", () => {
it(`filters out non-numeric values and calculates the median for [${input}]`, () => expect(calculateMedian(input)).toEqual(expected))
);
});

22 changes: 21 additions & 1 deletion Sprint-1/implement/dedupe.js
Original file line number Diff line number Diff line change
@@ -1 +1,21 @@
function dedupe() {}
function dedupe(array) {
const createSet = new Set()
const newArray = []
if(!Array.isArray(array) || array.length ===0) {
return []
}
for (let i = 0 ; i < array.length; i++) {
const element = array[i]
if (!createSet.has(element)) {
createSet.add(element)
newArray.push(element)
}
}
return newArray
}

console.log(dedupe([1,3,4,3,3,1,6,6]))
console.log(dedupe([1,3,4,6,7,8]))
console.log(dedupe(['a','a','a','c','c','g','g',5,5]))

module.exports = dedupe
30 changes: 26 additions & 4 deletions Sprint-1/implement/dedupe.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,34 @@ 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");
describe('dedupe function' , () => {
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
// Combined test for various deduplication scenarios using test.each
// Data format: [inputArray, expectedOutputArray, description]
// Given an array with no duplicates
// When passed to the dedupe function
// Then it should return a copy of 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
// 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.each([
[[1, 3, 6], [1, 3, 6], 'array with no duplicates'],
[['a', 'k', 'k', 'c', 'c'], ['a', 'k', 'c'], 'strings with duplicates'],
[[1, 1, 1, 3, 4, 4], [1, 3, 4], 'numbers with duplicates'],

// Examples from problem description:
[[5, 1, 1, 2, 3, 2, 5, 8], [5, 1, 2, 3, 8], 'mixed numbers with duplicates'],
[[1, 2, 1], [1, 2], 'simple number array with duplicates'],
[['hello', 'world', 'hello', '!', 'world'], ['hello', 'world', '!'], 'mixed string array with duplicates'],
[[null, undefined, null, 1, undefined], [null, undefined, 1], 'array with null/undefined duplicates'],
])('should deduplicate %j and return %j (%s)', (inputArray, expectedOutput, description) => {
expect(dedupe(inputArray)).toEqual(expectedOutput);
});
});
20 changes: 20 additions & 0 deletions Sprint-1/implement/max.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,24 @@
function findMax(elements) {
let max = -Infinity
for( let i = 0 ; i < elements.length; i++) {
const checkElements = elements[i]
if(typeof checkElements === "number" && Number.isFinite(checkElements)) {
if(checkElements > max){
max = checkElements
}
}
}
return max

}

console.log(findMax([2,5,4,1,8]))
console.log(findMax([2,5,4,8,1,,'b','j']))
console.log(findMax([]))
console.log(findMax([3]))
console.log(findMax([-1,-5,-9-150]))
console.log(findMax([0.1,0.100,0.5]))
console.log(findMax([2,5,-4,4,-1,1,10]))
console.log(findMax(['a','n','/','+']))

module.exports = findMax;
30 changes: 24 additions & 6 deletions Sprint-1/implement/max.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,32 +12,50 @@ We have set things up already so that this file can see your function from the o

const findMax = require("./max.js");

describe('findMax function' , ()=> {
// Given an empty array
// 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 both positive and negative numbers" , ()=> {
expect(findMax([4])).toBe(4)
})
// 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", ()=> {
expect(findMax([1,14,-4,-6,-11])).toBe(14)
})
// 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", ()=> {
expect(findMax([-1,-6,-50])).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", ()=> {
expect(findMax([0.1,0.100,0.5,])).toBe(0.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-number values", ()=> {
expect(findMax(['a','b','/',5,9,-1])).toBe(9)
})
// 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', ()=> {
expect(findMax(['o','l','+',')'])).toBe(-Infinity)
})

});
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 filterNumbers = elements.filter(
(element) => typeof element === "number" && Number.isFinite(element)
);
if (filterNumbers.length === 0) {
return 0;
}
const sumTotal = filterNumbers.reduce((acc, num) => acc + num);
return sumTotal;
}

console.log(sum([1, 5, 8, 3]));
console.log(sum([-1, -40, -10, -3]));
console.log(sum([]));
console.log(sum([50]));
console.log(sum(["a", 4, "/", 6, "+"]));
console.log(sum(["r", ")", "}", "#"]));
console.log(sum([0.5, 0.7, 0.5, 0.9]));
module.exports = sum;
27 changes: 21 additions & 6 deletions Sprint-1/implement/sum.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,28 +9,43 @@ E.g. sum(['hey', 10, 'hi', 60, 10]), target output: 80 (ignore any non-numerical
const sum = require("./sum.js");

// Acceptance Criteria:

describe('function sum', () => {
// 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", () => {
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 containing negative numbers", () => {
expect(sum([-3,-5,-8,-4])).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/float numbers", () => {
expect(sum([0.5, 0.7, 0.5, 0.9])).toBe(2.6)
})
// 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", () => {
expect(sum(["r", ")",5, "}",1, "#"])).toBe(6)
})
// 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", () => {
expect(sum(["r", ")", "}", "#"])).toBe(0)
})

});
4 changes: 2 additions & 2 deletions Sprint-1/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion Sprint-1/package.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"name": "week-1",
"name": "sprint-1",
"version": "1.0.0",
"description": "This README will guide you through the different sections for this week.",
"main": "writers.js",
Expand Down
13 changes: 10 additions & 3 deletions Sprint-1/refactor/includes.js
Original file line number Diff line number Diff line change
@@ -1,13 +1,20 @@
// 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) {

for (const item of list) {
if (item === target) {
return true;
}
}
return false;
}

console.log(includes([5, 6, null], 4));
console.log(includes(["a", "b", "m"], "m"));
console.log(includes(["a", "b", "m"], 2));
console.log(includes([1, 2, 2, 3], 2));
console.log(includes([null], null));
console.log(includes([], 2));

module.exports = includes;
20 changes: 20 additions & 0 deletions Sprint-1/stretch/aoc-2018-day1/solution.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
const fs = require('fs');
const path = require('path');
const inputFilePath = path.join(__dirname, 'input.txt');

const inputString = fs.readFileSync(inputFilePath, 'utf8');

function solvePart1(inputString) {
const changes = inputString.trim().split('\n').filter(line => line.length > 0);
const numericChanges = changes.map(change => Number(change));
let currentFrequency = 0;

for (const change of numericChanges) {
currentFrequency += change;
}

return currentFrequency;
}

const finalFrequency = solvePart1(inputString);
console.log(finalFrequency);