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
18 changes: 16 additions & 2 deletions Sprint-1/fix/median.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +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];
if (
!Array.isArray(list) ||
list.length === 0 ||
!list.some((value) => typeof value === "number")
) {
return null;
}
let newArray = list.slice();
//1 2 3 4
const filteredResult = newArray.filter((word) => typeof word === "number");
const orderedList = filteredResult.sort((a, b) => a - b);
const middleIndex = Math.floor(orderedList.length / 2);
if (orderedList.length % 2 === 0) {
return (orderedList[middleIndex - 1] + orderedList[middleIndex]) / 2;
}
const median = orderedList[middleIndex];
return median;
}

Expand Down
22 changes: 17 additions & 5 deletions Sprint-1/fix/median.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,8 @@ describe("calculateMedian", () => {
{ input: [1, 2, 3, 4], expected: 2.5 },
{ input: [1, 2, 3, 4, 5, 6], expected: 3.5 },
].forEach(({ input, expected }) =>
it(`returns the median for [${input}]`, () => expect(calculateMedian(input)).toEqual(expected))
it(`returns the median for [${input}]`, () =>
expect(calculateMedian(input)).toEqual(expected))
);

[
Expand All @@ -24,7 +25,8 @@ describe("calculateMedian", () => {
{ 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(`returns the correct median for unsorted array [${input}]`, () =>
expect(calculateMedian(input)).toEqual(expected))
);

it("doesn't modify the input array [3, 1, 2]", () => {
Expand All @@ -33,8 +35,17 @@ describe("calculateMedian", () => {
expect(list).toEqual([3, 1, 2]);
});

[ 'not an array', 123, null, undefined, {}, [], ["apple", null, undefined] ].forEach(val =>
it(`returns null for non-numeric array (${val})`, () => expect(calculateMedian(val)).toBe(null))
[
"not an array",
123,
null,
undefined,
{},
[],
["apple", null, undefined],
].forEach((val) =>
it(`returns null for non-numeric array (${val})`, () =>
expect(calculateMedian(val)).toBe(null))
);

[
Expand All @@ -45,6 +56,7 @@ describe("calculateMedian", () => {
{ input: [3, "apple", 1, null, 2, undefined, 4], expected: 2.5 },
{ input: ["banana", 5, 3, "apple", 1, 4, 2], expected: 3 },
].forEach(({ input, expected }) =>
it(`filters out non-numeric values and calculates the median for [${input}]`, () => expect(calculateMedian(input)).toEqual(expected))
it(`filters out non-numeric values and calculates the median for [${input}]`, () =>
expect(calculateMedian(input)).toEqual(expected))
);
});
13 changes: 12 additions & 1 deletion Sprint-1/implement/dedupe.js
Original file line number Diff line number Diff line change
@@ -1 +1,12 @@
function dedupe() {}
function dedupe(list) {
let result = [];

for (let item of list) {
if (!result.includes(item)) {
result.push(item);
}
}
return result;
}

module.exports = dedupe;
17 changes: 15 additions & 2 deletions Sprint-1/implement/dedupe.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,25 @@ 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", () => {
const currentValue = dedupe([]);
const targetValue = [];
expect(currentValue).toEqual(targetValue);
});

// 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 orginal array", () => {
const currentValue = dedupe([1, "1", 2]);
const targetValue = [1, "1", 2];
expect(currentValue).toEqual(targetValue);
});
// 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 strings/numbers, it removes duplicate values while keeping the first occurence of each element", () => {
const currentValue = dedupe(["hello", "hello", "world", 1, 1, 3]);
const targetValue = ["hello", "world", 1, 3];
expect(currentValue).toEqual(targetValue);
});
11 changes: 10 additions & 1 deletion Sprint-1/implement/max.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,13 @@
function findMax(elements) {
}
const filteredValue = elements.filter(
(value) => typeof value === "number" && !isNaN(value)
);

if (filteredValue.length === 0 && elements.length > 0) {
return NaN;
}

return Math.max(...filteredValue);
}
console.log(findMax(["hello", "hi", "me"]));
module.exports = findMax;
41 changes: 35 additions & 6 deletions 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", () => {
const currentValue = findMax([]);
const TargetValue = -Infinity;
expect(currentValue).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 one number i.e 20, returns that number", () => {
const currentValue = findMax([20]);
const targetValue = 20;
expect(currentValue).toBe(targetValue);
});
// 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 i.e [1,2,-3,-4], it returns the largest number", () => {
const currentValue = findMax([1, 2, -3, -4]);
const targetValue = 2;
expect(currentValue).toBe(targetValue);
});
// 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, it returns closest number to zero", () => {
const currentValue = findMax([-1, -2, -3, -4, -5]);
const targetValue = -1;
expect(currentValue).toBe(targetValue);
});
// 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 i.e[1.4,6.5,3.2,4.8], it returns the largest decimal i.e 6.5", () => {
const currentValue = findMax([1.4, 6.5, 3.2, 4.8]);
const targetValue = 6.5;
expect(currentValue).toBe(targetValue);
});
// 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, it returns max and ignores non numeric values", () => {
const currentValue = findMax([1, 5, 10, "hello", "h", "."]);
const targetValue = 10;
expect(currentValue).toBe(targetValue);
});
// 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, it returns the least surprising value", () => {
const currentValue = findMax(["hello", "world", "1", "/"]);
const targetValue = NaN;
expect(currentValue).toBe(targetValue);
});
11 changes: 11 additions & 0 deletions Sprint-1/implement/sum.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,15 @@
function sum(elements) {
let sortedElements = elements.filter(
(value) => typeof value === "number" && !isNaN(value)
);
if (sortedElements.length === 0 && elements.length > 0) {
return NaN;
}
let total = 0;
for (let i = 0; i < sortedElements.length; i++) {
total += sortedElements[i];
}
return Number(total.toFixed(4));
}

module.exports = sum;
35 changes: 30 additions & 5 deletions Sprint-1/implement/sum.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,24 +13,49 @@ 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", () => {
const currentValue = sum([]);
const targetValue = 0;
expect(currentValue).toBe(targetValue);
});

// 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, it returns that number", () => {
const currentValue = sum([2]);
const targetValue = 2;
expect(currentValue).toBe(targetValue);
});
// 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, it returns the correct total sum", () => {
const currentValue = sum([-1, -5, -10, -5]);
const targetValue = -21;
expect(currentValue).toBe(targetValue);
});
// 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, it returns the correct total sum", () => {
const currentValue = sum([1.2, 2.2, 3.3, 4.4]);
const targetValue = 11.1;
expect(currentValue).toBe(targetValue);
});
// 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, it returns sum of numerical elements", () => {
const currentValue = sum(["hello", "hi", "3", 1, 2, 3]);
const targetValue = 6;
expect(currentValue).toBe(targetValue);
});
// 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 containing non-number values, it returns least suprising value", () => {
const currentValue = sum(["h", "3", "w", "hello"]);
const targetValue = NaN;
expect(currentValue).toBe(targetValue);
});
7 changes: 3 additions & 4 deletions Sprint-1/refactor/includes.js
Original file line number Diff line number Diff line change
@@ -1,13 +1,12 @@
// 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 (let item of list) {
if (target === item) {
return true;
}
}
return false;
}

console.log(includes(["a", "b", "c", "d"], "c"));
module.exports = includes;
5 changes: 4 additions & 1 deletion Sprint-2/implement/lookup.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
function createLookup() {
function createLookup(countryCurrency) {
// implementation here
for(let value of countryCurrency) {
const [key,value] =
}
}

module.exports = createLookup;
44 changes: 43 additions & 1 deletion Sprint-3/alarmclock/alarmclock.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,46 @@
function setAlarm() {}
let countDown;
let remainingSeconds = 0;

function setAlarm() {
let timeRemaining = document.getElementById("timeRemaining");
let alarmSet = document.getElementById("alarmSet").value;
remainingSeconds = Number(alarmSet);
if (isNaN(remainingSeconds) || remainingSeconds <= 0) {
prompt("please enter a value greater than zero!");
return;
}
displayTime();
countDown = setInterval(timer, 1000);
}

function displayTime() {
let timeRemaining = document.getElementById("timeRemaining");
let hour = Math.floor(remainingSeconds / 3600);
let minutes = Math.floor((remainingSeconds % 3600) / 60);
let seconds = Math.floor(remainingSeconds % 60);

if (minutes < 10) {
minutes = "0" + minutes;
}
if (seconds < 10) {
seconds = "0" + seconds;
}

if (hour > 0) {
timeRemaining.textContent = `Time Remaining: ${hour}:${minutes}:${seconds}`;
} else {
timeRemaining.textContent = `Time Remaining: ${minutes}:${seconds}`;
}
}

function timer() {
remainingSeconds--;
displayTime();
if (remainingSeconds <= 0) {
clearInterval(countDown);
playAlarm();
}
}

// DO NOT EDIT BELOW HERE

Expand Down
2 changes: 1 addition & 1 deletion Sprint-3/alarmclock/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
<meta charset="utf-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<link rel="stylesheet" href="style.css" />
<title>Title here</title>
<title>Alarm clock app</title>
</head>
<body>
<div class="centre">
Expand Down
Loading
Loading