Skip to content
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
41 changes: 31 additions & 10 deletions Sprint-1/fix/median.js
Original file line number Diff line number Diff line change
@@ -1,14 +1,35 @@
// Fix this implementation
// Start by running the tests for this function
// If you're in the Sprint-1 directory, you can run `npm test -- fix` to run the tests in the fix directory

// Hint: Please consider scenarios when 'list' doesn't have numbers (the function is expected to return null)
// or 'list' has mixed values (the function is expected to sort only numbers).
function sorting(arr) {
for (let i = 0; i < arr.length - 1; i++) {
let maxIndex = i;
for (let j = i + 1; j < arr.length; j++) {
if (arr[j] > arr[maxIndex]) {
maxIndex = j;
}
}
if (maxIndex !== i) {
[arr[i], arr[maxIndex]] = [arr[maxIndex], arr[i]];
}
}
return arr}

function calculateMedian(list) {
const middleIndex = Math.floor(list.length / 2);
const median = list.splice(middleIndex, 1)[0];
return median;
let arr = [];
if (!Array.isArray(list))
return null;
for (let i = 0; i < list.length; i++) {
if (typeof list[i] === "number")
arr.push(list[i]);
}
if (arr.length === 0)
return null;
sorting(arr);
if (arr.length % 2 === 0)
return (arr[arr.length / 2 ] + arr[arr.length / 2 - 1]) / 2;
else
return arr[Math.floor(arr.length / 2)];
}

module.exports = calculateMedian;
const array = [1, 2, 3, 4, "apple", 6];
calculateMedian(array);

module.exports = calculateMedian;
2 changes: 2 additions & 0 deletions Sprint-1/fix/median.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -48,3 +48,5 @@ describe("calculateMedian", () => {
it(`filters out non-numeric values and calculates the median for [${input}]`, () => expect(calculateMedian(input)).toEqual(expected))
);
});


1 change: 1 addition & 0 deletions Sprint-1/implement/dedupe.js
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
function dedupe() {}

36 changes: 30 additions & 6 deletions Sprint-1/implement/dedupe.test.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,12 @@
const dedupe = require("./dedupe.js");
/*
Dedupe Array
// dedupe.js
const dedupe = (arr) => {
return [...new Set(arr)];
};

module.exports = dedupe;

📖 Dedupe means **deduplicate**

Expand All @@ -18,10 +24,28 @@ E.g. dedupe([1, 2, 1]) target output: [1, 2]
// Then it should return an empty array
test.todo("given an empty array, it returns an empty array");

// Given an array with no duplicates
// When passed to the dedupe function
// Then it should return a copy of the original array
// dedupe.test.js
const dedupe = require("./dedupe.js");

// 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
describe("Dedupe Function", () => {
test("given an empty array, it returns an empty array", () => {
expect(dedupe([])).toEqual([]);
});

test("given an array with no duplicates, it returns a copy of the original array", () => {
const input = [1, 2, 3];
expect(dedupe(input)).toEqual([1, 2, 3]);
});

test("given an array with strings, it removes the duplicate values", () => {
expect(dedupe(['a', 'a', 'a', 'b', 'b', 'c'])).toEqual(['a', 'b', 'c']);
});

test("given an array with numbers, it removes the duplicate values", () => {
expect(dedupe([5, 1, 1, 2, 3, 2, 5, 8])).toEqual([5, 1, 2, 3, 8]);
});

test("given an array with mixed types, it removes duplicates while preserving order", () => {
expect(dedupe([1, 'a', 'b', 1, 2, 'a'])).toEqual([1, 'a', 'b', 2]);
});
});
2 changes: 2 additions & 0 deletions Sprint-1/implement/max.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
function findMax(elements) {
}


module.exports = findMax;

46 changes: 46 additions & 0 deletions Sprint-1/implement/max.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,23 @@ We have set things up already so that this file can see your function from the o
const findMax = require("./max.js");

// 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");
// max.js
const max = (arr) => {
if (arr.length === 0) return -Infinity; // Return -Infinity for empty arrays

const validNumbers = arr.filter(num => typeof num === 'number' && !isNaN(num)); // Filter valid numbers

if (validNumbers.length === 0) return NaN; // Return NaN if no valid numbers

return Math.max(...validNumbers); // Return the maximum value
};

module.exports = max;

// Given an array with one number
// When passed to the max function
Expand All @@ -41,3 +54,36 @@ test.todo("given an empty array, returns -Infinity");
// 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
// max.test.js
const max = require("./max.js");

describe("Max Function", () => {
test("given an empty array, returns -Infinity", () => {
expect(max([])).toBe(-Infinity);
});

test("given an array with one number, returns that number", () => {
expect(max([5])).toBe(5);
});

test("given an array with both positive and negative numbers, returns the largest number overall", () => {
expect(max([-5, 0, 15, 7])).toBe(15);
});

test("given an array with just negative numbers, returns the closest one to zero", () => {
expect(max([-3, -9, -1])).toBe(-1);
});

test("given an array with decimal numbers, returns the largest decimal number", () => {
expect(max([2.5, 3.1, 2.9])).toBe(3.1);
});

test("given an array with non-number values, returns the max and ignores non-numeric values", () => {
expect(max([5, 'a', 2, null, 10])).toBe(10);
});

test("given an array with only non-number values, returns NaN", () => {
expect(max(['a', null, 'b'])).toBeNaN();
});
});

1 change: 1 addition & 0 deletions Sprint-1/implement/sum.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
function sum(elements) {
}


module.exports = sum;
42 changes: 42 additions & 0 deletions Sprint-1/implement/sum.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,18 @@ const sum = require("./sum.js");
// When passed to the sum function
// Then it should return 0
test.todo("given an empty array, returns 0")
// sum.js
const sum = (arr) => {
// If the input array is empty, return 0
if (arr.length === 0) return 0;

// Filter for valid numbers and sum them up
return arr
.filter(num => typeof num === 'number' && !isNaN(num)) // Keep only numbers
.reduce((total, current) => total + current, 0); // Reduce to the sum
};

module.exports = sum;

// Given an array with just one number
// When passed to the sum function
Expand All @@ -34,3 +46,33 @@ test.todo("given an empty array, returns 0")
// 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
// sum.test.js
const sum = require("./sum.js");

describe("Sum Function", () => {
test("given an empty array, returns 0", () => {
expect(sum([])).toBe(0);
});

test("given an array with just one number, returns that number", () => {
expect(sum([5])).toBe(5);
});

test("given an array containing negative numbers, returns the correct total sum", () => {
expect(sum([-5, -10, 5])).toBe(-10);
});

test("given an array with decimal/float numbers, returns the correct total sum", () => {
expect(sum([1.5, 2.5, 3.0])).toBe(7.0);
});

test("given an array containing non-number values, ignores non-numerical values and returns the sum of the numerical elements", () => {
expect(sum(['hey', 10, 'hi', 60, 10])).toBe(80);
});

test("given an array with only non-number values, returns 0", () => {
expect(sum(['a', null, 'b'])).toBe(0);
});
});


2 changes: 2 additions & 0 deletions Sprint-1/package-lock.json

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

1 change: 1 addition & 0 deletions Sprint-1/refactor/includes.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,4 +10,5 @@ function includes(list, target) {
return false;
}


module.exports = includes;
1 change: 1 addition & 0 deletions Sprint-1/refactor/includes.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -36,3 +36,4 @@ test("searches for null", () => {

expect(currentOutput).toEqual(targetOutput);
});