Skip to content
This repository was archived by the owner on Dec 18, 2024. It is now read-only.
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
18 changes: 15 additions & 3 deletions week-1/fix/median.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,22 @@
// Start by running the tests for this function
// If you're in the week-1 directory, you can run npm test -- fix to run the tests in the fix directory

// function calculateMedian(list) {
// const middleIndex = Math.floor(list.length / 2);
// const median = list.splice(middleIndex, 1)[0];
// return median;
// }

function calculateMedian(list) {
const middleIndex = Math.floor(list.length / 2);
const median = list.splice(middleIndex, 1)[0];
return median;
let count = 0;

for (item of list) {
count += item;
}

const middleIndex = count / list.length;
return middleIndex;
}


module.exports = calculateMedian;
11 changes: 10 additions & 1 deletion week-1/implement/dedupe.js
Original file line number Diff line number Diff line change
@@ -1 +1,10 @@
function dedupe() {}
function dedupe(array) {

const uniqueElements = new Set(array);

const dedupedArray = [...uniqueElements];

return dedupedArray;
}

module.exports = dedupe;
18 changes: 17 additions & 1 deletion week-1/implement/dedupe.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,28 @@ E.g. dedupe([5, 1, 1, 2, 3, 2, 5, 8]) target output: [5, 1, 2, 3, 8]
// 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.todo("given an empty array, it returns an empty array", () =>{
const input = [];
const result = dedupe(input);
expect(result).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, it returns a copy of the original array", () => {
const input = ['a', 'b', 'c'];
const result = dedupe(input);
expect(result).toEqual(['a', 'b', 'c']);
});

// Given an array with strings or numbers
// When passed to the dedupe function
// Then it should remove the duplicate values

test("given an array with strings or numbers, it should remove the duplicate values", () => {
const input = [5, 1, 1, 2, 3, 2, 5, 8];
const result = dedupe(input);
expect(result).toEqual([5, 1, 2, 3, 8]);
});
14 changes: 14 additions & 0 deletions week-1/implement/max.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
function max(numbers) {
const largestNumber = Math.max(...numbers);
for (element of numbers) {
if (typeof element === "string") {
const arrWithoutStrings = numbers.filter(
(strings) => typeof strings === "number"
);
return Math.max(...arrWithoutStrings);
}
}
return largestNumber;
}

module.exports = max;
31 changes: 30 additions & 1 deletion week-1/implement/max.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,20 +9,49 @@ E.g. max(['hey', 10, 'hi', 60, 10]), target output: 60 (sum ignores any non-nume
// Given an empty array
// When passed to the max function
// Then it should return -Infinity
test.todo("given an empty array, returns -Infinity");


// Given an array with one number
// When passed to the max function
// Then it should return that number


// Given an array with both positive and negative numbers
// When passed to the max function
// Then it should return the largest number overall


// Given an array with decimal numbers
// When passed to the max function
// Then it should return the largest decimal number


// Given an array with non-number values
// When passed to the max function
// Then it should return the max and ignore non-numeric values


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

test("given an empty array returns -infinity", function () {
expect(max([])).toBe(-Infinity);
});


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



test("given an array with + & - numbers it returns the largest number overall", function () {
expect(max([-1, 2, 25, -3, 0])).toBe(25);
});

test("returns the largest from a decimal number array", function () {
expect(max([1.1, 2.3, 25.8, 3.9, 0.3])).toBe(25.8);
});

test("seperates strings from numbers and then identifies the largest number", function () {
expect(max(["hi", 2.3, 62, "q", 3.9, 0.3])).toBe(62);
});
18 changes: 18 additions & 0 deletions week-1/implement/sum.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
function sum(array) {

let total = 0;


for (const element of array) {

if (typeof element === 'number') {

total += element;
}
}


return total;
}

module.exports = sum;
32 changes: 32 additions & 0 deletions week-1/implement/sum.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,22 +8,54 @@ E.g. sum(['hey', 10, 'hi', 60, 10]), target output: 80 (ignore any non-numerical

// Acceptance Criteria:

const sum = require("./sum.js");

// Given an empty array
// When passed to the sum function
// Then it should return 0

test("given an empty array, it returns 0", () => {
const input = [];
const result = sum(input);
expect(result).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, it returns that number", () => {
const input = [42];
const result = sum(input);
expect(result).toBe(42);
});

// 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 input = [10, -5, 20, 0, -15];
const result = sum(input);
expect(result).toBe(10);
});

// 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 input = [2.5, 1.1, 3.0, 2.7];
const result = sum(input);
expect(result).toBe(9.3);
});

// 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 ignores non-numerical values and returns the sum of the numerical elements", () => {
const input = ['hey', 10, 'hi', 60, 10, 'oops', '42.5'];
const result = sum(input);
expect(result).toBe(80);
});
9 changes: 3 additions & 6 deletions week-1/refactor/find.js
Original file line number Diff line number Diff line change
@@ -1,13 +1,10 @@
// Refactor the implementation of find to use a for...of loop

function find(list, target) {
for (let index = 0; index < list.length; index++) {
const element = list[index];
if (element === target) {
return index;
}
if (list.includes(target)) {
return list.indexOf(target);
}
return -1;
return -1;
}

module.exports = find;
3 changes: 2 additions & 1 deletion week-2/debug/address.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,4 +12,5 @@ const address = {
postcode: "XYZ 123",
};

console.log(`My house number is ${address[0]}`);
// console.log(`My house number is ${address[0]}`);
console.log(`My house number is ${address.houseNumber}`);
11 changes: 9 additions & 2 deletions week-2/debug/author.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
// This program attempts to log out all the property values in the object.
// But it isn't working. Explain why first and then fix the problem

//It is not working because the for loop is trying to iterate the object author which is not iterable. Instead we need to link it to the values inside the array.

const author = {
firstName: "Zadie",
lastName: "Smith",
Expand All @@ -11,6 +13,11 @@ const author = {
alive: true,
};

for (const value of author) {
console.log(value);
// for (const value of author) {
// console.log(value);
// }

for (const value of Object.values(author)) {

console.log(value);
}
8 changes: 6 additions & 2 deletions week-2/debug/recipe.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,10 @@ const recipe = {
ingredients: ["olive oil", "tomatoes", "salt", "pepper"],
};

// console.log(`${recipe.title} serves ${recipe.serves}
// ingredients:
// ${recipe}`);

console.log(`${recipe.title} serves ${recipe.serves}
ingredients:
${recipe}`);
ingredients:
${recipe.ingredients.join("\n")}`);
10 changes: 9 additions & 1 deletion week-2/implement/contains.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,11 @@
function contains() {}
function contains(x, contains) {
if (
Object.values(x).includes(contains) ||
Object.keys(x).includes(contains)
) {
return true;
}
return false;
}

module.exports = contains;
20 changes: 20 additions & 0 deletions week-2/implement/contains.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,18 +17,38 @@ as the object doesn't contains a key of 'c'
// When passed an object and a property name
// Then it should return true if the object contains the property, false otherwise

test("returns true or false based on wether object includes specified char/num", function () {
expect(contains({ a: 1, b: 2, c: 3 }, "a")).toBe(true);
});
// Given an empty object
// When passed to contains
// Then it should return false

test("returns true or false based on wether object includes specified char/num", function () {
expect(contains({}, "a")).toBe(false);
});

// Given an object with properties
// When passed to contains with an existing property name
// Then it should return true

test("returns true or false based on wether object includes specified char/num", function () {
expect(contains({ a: 1, b: 2, c: 3 }, "a")).toBe(true);
});


// Given an object with properties
// When passed to contains with a non-existent property name
// Then it should return false

test("returns true or false based on wether object includes specified char/num", function () {
expect(contains({ a: 1, b: 2, c: 3 }, 5)).toBe(false);
});

// Given invalid parameters like arrays
// When passed to contains
// Then it should return false or throw an error

test("returns true or false based on wether object includes specified char/num", function () {
expect(contains([], 5)).toBe(false);
});
26 changes: 16 additions & 10 deletions week-2/implement/lookup.js
Original file line number Diff line number Diff line change
@@ -1,17 +1,23 @@
function createLookup() {
function createLookup(countryCurrencyPairs) {
// implementation here
const countryCurrencyAsObject = {}; // Creates an empty object
const [key, value] = countryCurrencyPairs;
countryCurrencyAsObject[key] = value;
return countryCurrencyAsObject;

}
module.exports = createLookup;

/* ======= Test suite is provided below... =====
*/

test("converts a single pair of currency codes", () => {
expect(createLookup([["GB", "GBP"]])).toEqual({
GB: "GBP",
});
expect(createLookup([["DE", "EUR"]])).toEqual({
DE: "EUR",
});
});
// test("converts a single pair of currency codes", () => {
// expect(createLookup([["GB", "GBP"]])).toEqual({
// GB: "GBP",
// });
// expect(createLookup([["DE", "EUR"]])).toEqual({
// DE: "EUR",
// });
// });

test.todo("creates a country currency code lookup for multiple codes");
// test.todo("creates a country currency code lookup for multiple codes");
5 changes: 5 additions & 0 deletions week-2/implement/lookup.test.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
const createLookup = require("./lookup");

/*

Create a lookup object of key value pairs from an array of code pairs
Expand Down Expand Up @@ -29,3 +31,6 @@ It should return:
'CA': 'CAD'
}
*/
test("converts a single pair of currency codes", () => {
expect(createLookup(["GB", "GBP"])).toEqual({ GB: "GBP" });
});
21 changes: 21 additions & 0 deletions week-2/implement/tally.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
function tally(arr) {
const counter = {};

if (typeof arr === "string") {
return "Error, you should pass an array of values";
}

arr.forEach((item) => {
if (counter[item]) {
counter[item] += 1;
} else {
counter[item] = 1;
}
});
return counter;
}

console.log(tally(["a", "a", "a", "hello", "bahadory", "bahadory", "marcus"]));
console.log(tally("hello"));

module.exports = tally;
Loading