Skip to content
Closed
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
34 changes: 31 additions & 3 deletions Sprint-1/fix/median.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,37 @@
// 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 valid input -array
if(!Array.isArray(list) || list.length === 0) return null;

// Filter non-numeric values

const numbers = list.filter(item => typeof item === 'number');
if (numbers.length === 0) return null;

// Sort the array

const sorted = [...numbers].sort((a, b) => a - b);

// Get the index of the middle item by rounding it using Math.floor()

const mid = Math.floor(sorted.length/2);

// for Odd number of items return the middle element

if (sorted.length % 2 !== 0) return sorted[mid];

// for even number of items
return (sorted[mid - 1] + sorted[mid]) / 2;

}

module.exports = calculateMedian;


// Implementation fixed

// const middleIndex = Math.floor(list.length / 2);
// const median = list.splice(middleIndex, 1)[0];
// return median;
2 changes: 1 addition & 1 deletion Sprint-1/fix/median.test.js
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))
);
});
});
9 changes: 8 additions & 1 deletion Sprint-1/implement/dedupe.js
Original file line number Diff line number Diff line change
@@ -1 +1,8 @@
function dedupe() {}
function dedupe(array) {
if (!Array.isArray(array)) throw new Error("Input must be an array");
return array.filter((item, index) => array.indexOf(item) === index);
}
console.log(dedupe(['a', 'a', 'a', 'b', 'b', 'c']));
module.exports = dedupe;

// Dedupe function implemented.
24 changes: 22 additions & 2 deletions Sprint-1/implement/dedupe.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,32 @@ 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 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']);
});

// 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
// Then it should remove the duplicate values, preserving the first occurrence of each element

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

//

test("Given an array removes duplicates but keeps first occurrence", () => {
expect(dedupe([1, 2, 1])).toEqual([1, 2]);
});

// Dedupe function implemented, test cases.. performed.
20 changes: 19 additions & 1 deletion Sprint-1/implement/max.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,22 @@
function findMax(elements) {
function findMax(list) {

// check if input is array

if (!Array.isArray(list)) {
throw new Error("Input must be an array");
}

// filter out non-numeric values
list = list.filter((list) => typeof list === "number");

if (list.length === 0) {
return -Infinity;
}
if (list.length === 1) {
return list[0];
}

return Math.max(...list);
}

module.exports = findMax;
31 changes: 30 additions & 1 deletion Sprint-1/implement/max.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,28 +16,57 @@ 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([42])).toEqual(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 positive and negative numbers, returns the largest", () => {
expect(findMax([-10, 20, 5, -1])).toEqual(20);
});

// 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 only negative numbers, returns the closest to zero", () => {
expect(findMax([-50, -10, -100])).toEqual(-10);
});

// 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.2, 3.5, 2.8])).toEqual(3.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, ignores them and returns the max number", () => {
expect(findMax(["a", 10, "b", 60, 10])).toEqual(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(["a", "b", null, undefined])).toEqual(-Infinity);
});

// max.test.js test cases performed and function implemented
3 changes: 3 additions & 0 deletions Sprint-1/implement/sum.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
function sum(elements) {
return elements
.filter(elements => typeof elements === "number" && !Number.isNaN(elements))
.reduce((total, elements) => total + elements, 0);
}

module.exports = sum;
25 changes: 24 additions & 1 deletion Sprint-1/implement/sum.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,24 +13,47 @@ 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 one number, returns that number", () => {
expect(sum([5])).toEqual(5);
});

// 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 sum", () => {
expect(sum([-5, 10, -5])).toEqual(0);
});

// Given an array with decimal/float numbers
// When passed to the sum function
// Then it should return the correct total sum

test("given decimal numbers, returns the correct sum", () => {
expect(sum([1.5, 2.5])).toEqual(4);
});

// 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 non-number values, ignores them and returns sum of numeric values", () => {
expect(sum(["hey", 10, "hi", 60, 10])).toEqual(80);
});

// 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 only non-number values, returns 0", () => {
expect(sum(["a", "b", null, undefined])).toEqual(0);
});

// Sum.test.js function implemented and tested.
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
3 changes: 3 additions & 0 deletions Sprint-1/refactor/includes.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -36,3 +36,6 @@ test("searches for null", () => {

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


// include.test.js passed the test after refactoring the include-function with (for ... of)
28 changes: 28 additions & 0 deletions Sprint-1/stretch/aoc-2018-day1/solution.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
// We need to import file system and path for our task

const fs = require("fs");
const path = require("path");

function solution() {

// set file path

const inputFilePath = path.join(__dirname, "input.txt");

// read the file content
let fileContent = fs.readFileSync(inputFilePath, "utf-8");

// split the content into lines and convert each line to a number
let numbersArray = fileContent.split("\n").map(num => Number(num));

// calculate the sum of all numbers
const sumOfNumbers = numbersArray.reduce((sum, num) => sum + num, 0);

return sumOfNumbers;
}

console.log(solution());

module.exports = solution;

// A solution to add-up frequency values in input.txt