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
26 changes: 23 additions & 3 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) {
const middleIndex = Math.floor(list.length / 2);
const median = list.splice(middleIndex, 1)[0];
return median;
// Check if input is an array
if (!Array.isArray(list)) {
return null;
} else {
// Get only numeric values
const numericList = list.filter((item) => typeof item === "number");
// Check if its empty after filtering
if (numericList.length === 0) {
return null;
}
// Sort the list by numeric value
numericList.sort((a, b) => a - b);
// Get middle index
const middleIndex = Math.floor(numericList.length / 2);
// Check if length is even or odd
if (numericList.length % 2 === 0) {
// Return average of two middle numbers
return (numericList[middleIndex - 1] + numericList[middleIndex]) / 2;
} else {
// Return middle number by index
return numericList[middleIndex];
}
}
}

module.exports = calculateMedian;
24 changes: 23 additions & 1 deletion Sprint-1/implement/dedupe.js
Original file line number Diff line number Diff line change
@@ -1 +1,23 @@
function dedupe() {}
function dedupe(input) {
// Check if the input is an array
if (!Array.isArray(input)) {
return null;
}
// Check for empty array
if (input.length === 0) {
return [];
}
// New array to hold deduped values
const newArray = [];
// Go through each element in the input array
for (let i = 0; i < input.length; i++) {
const element = input[i];
// If the element is not already in newArray, add it
if (!newArray.includes(element)) {
newArray.push(element);
}
}
return newArray;
}

module.exports = dedupe;
21 changes: 20 additions & 1 deletion Sprint-1/implement/dedupe.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@ 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");

// Given an array with no duplicates
// When passed to the dedupe function
Expand All @@ -25,3 +24,23 @@ test.todo("given an empty array, it returns an empty 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

describe("dedupe", () => {
[
{ input: [], expected: [] },
{ input: ["apple", "banana", 1, 10], expected: ["apple", "banana", 1, 10] },
{
input: [1, -1, -100, -100, "apple", "apple"],
expected: [1, -1, -100, "apple"],
},
{ input: [-10, -20, -3, -4], expected: [-10, -20, -3, -4] },
{ input: [1.5, 10.5, 0.5], expected: [1.5, 10.5, 0.5] },
{
input: ["apple", "banana", "cherry"],
expected: ["apple", "banana", "cherry"],
},
].forEach(({ input, expected }) =>
it(`returns the deduped array for [${input}]`, () =>
expect(dedupe(input)).toEqual(expected))
);
});
12 changes: 12 additions & 0 deletions Sprint-1/implement/max.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,16 @@
function findMax(elements) {
// Check if input is an array
if (!Array.isArray(elements)) {
return null;
}
// Filter only numeric values
const numericElements = elements.filter((item) => typeof item === "number");
// Check if array is empty after filtering
if (numericElements.length === 0) {
return -Infinity;
}
// Return the maximum value
return Math.max(...numericElements);
}

module.exports = findMax;
14 changes: 13 additions & 1 deletion Sprint-1/implement/max.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@ 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");

// Given an array with one number
// When passed to the max function
Expand All @@ -41,3 +40,16 @@ 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

describe("findMax", () => {
[
{ input: ["apple", "banana", 1], expected: 1 },
{ input: [1, -1, -100], expected: 1 },
{ input: [-10, -20, -3, -4], expected: -3 },
{ input: [1.5, 10.5, 0.5], expected: 10.5 },
{ input: ["apple", "banana", "cherry"], expected: -Infinity },
].forEach(({ input, expected }) =>
it(`returns the maximum for [${input}]`, () =>
expect(findMax(input)).toEqual(expected))
);
});
19 changes: 19 additions & 0 deletions Sprint-1/implement/sum.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,23 @@
function sum(elements) {
// Check if input is an array
if (!Array.isArray(elements)) {
return null;
}
// Check if array is empty
if (elements.length === 0) {
return 0;
}
// Filter only numeric values
const numericElements = elements.filter((item) => typeof item === "number");
if (numericElements.length === 0) {
return -Infinity;
}
// Calculate the sum of numeric values
let total = 0;
for (let i = 0; i < numericElements.length; i++) {
total += numericElements[i];
}
return total;
}

module.exports = sum;
14 changes: 13 additions & 1 deletion Sprint-1/implement/sum.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ 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")

// Given an array with just one number
// When passed to the sum function
Expand All @@ -34,3 +33,16 @@ 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

describe("sum", () => {
[
{ input: ["apple", "banana", 1, 10], expected: 11 },
{ input: [1, -1, -100], expected: -100 },
{ input: [-10, -20, -3, -4], expected: -37 },
{ input: [1.5, 10.5, 0.5], expected: 12.5 },
{ input: ["apple", "banana", "cherry"], expected: -Infinity },
].forEach(({ input, expected }) =>
it(`returns the sum for [${input}]`, () =>
expect(sum(input)).toEqual(expected))
);
});
3 changes: 1 addition & 2 deletions Sprint-1/refactor/includes.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
// 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];
for (const element of list) {
if (element === target) {
return true;
}
Expand Down
Loading