From f765c996394188d19f8a9c9450a961ed680e91e6 Mon Sep 17 00:00:00 2001 From: karam Date: Fri, 10 Nov 2023 16:53:40 +0000 Subject: [PATCH 1/5] completed median.js --- week-1/fix/median.js | 18 +++++++++++++++--- 1 file changed, 15 insertions(+), 3 deletions(-) diff --git a/week-1/fix/median.js b/week-1/fix/median.js index 046be3d0..dc74e432 100644 --- a/week-1/fix/median.js +++ b/week-1/fix/median.js @@ -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; From 60410d58bbd6f9e6968c959f79a24bdf42dd0b91 Mon Sep 17 00:00:00 2001 From: karam Date: Fri, 10 Nov 2023 19:42:53 +0000 Subject: [PATCH 2/5] completed implement and refactor exercises --- week-1/implement/dedupe.js | 11 ++++++++++- week-1/implement/dedupe.test.js | 18 +++++++++++++++++- week-1/implement/max.js | 14 ++++++++++++++ week-1/implement/max.test.js | 31 ++++++++++++++++++++++++++++++- week-1/implement/sum.js | 18 ++++++++++++++++++ week-1/implement/sum.test.js | 32 ++++++++++++++++++++++++++++++++ week-1/refactor/find.js | 9 +++------ 7 files changed, 124 insertions(+), 9 deletions(-) diff --git a/week-1/implement/dedupe.js b/week-1/implement/dedupe.js index 781e8718..ebe12056 100644 --- a/week-1/implement/dedupe.js +++ b/week-1/implement/dedupe.js @@ -1 +1,10 @@ -function dedupe() {} +function dedupe(array) { + + const uniqueElements = new Set(array); + + const dedupedArray = [...uniqueElements]; + + return dedupedArray; +} + +module.exports = dedupe; \ No newline at end of file diff --git a/week-1/implement/dedupe.test.js b/week-1/implement/dedupe.test.js index 145f4440..6493716d 100644 --- a/week-1/implement/dedupe.test.js +++ b/week-1/implement/dedupe.test.js @@ -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]); + }); \ No newline at end of file diff --git a/week-1/implement/max.js b/week-1/implement/max.js index e69de29b..accd15a1 100644 --- a/week-1/implement/max.js +++ b/week-1/implement/max.js @@ -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; \ No newline at end of file diff --git a/week-1/implement/max.test.js b/week-1/implement/max.test.js index b1ba2556..728a967e 100644 --- a/week-1/implement/max.test.js +++ b/week-1/implement/max.test.js @@ -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); + }); \ No newline at end of file diff --git a/week-1/implement/sum.js b/week-1/implement/sum.js index e69de29b..000061a6 100644 --- a/week-1/implement/sum.js +++ b/week-1/implement/sum.js @@ -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; \ No newline at end of file diff --git a/week-1/implement/sum.test.js b/week-1/implement/sum.test.js index 6b623592..9295d90a 100644 --- a/week-1/implement/sum.test.js +++ b/week-1/implement/sum.test.js @@ -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); + }); \ No newline at end of file diff --git a/week-1/refactor/find.js b/week-1/refactor/find.js index 7df447b9..5f35976c 100644 --- a/week-1/refactor/find.js +++ b/week-1/refactor/find.js @@ -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; From 0c081ceaa43ff572b256874691d483e3371f49a3 Mon Sep 17 00:00:00 2001 From: karam Date: Mon, 13 Nov 2023 11:43:26 +0000 Subject: [PATCH 3/5] fixed address.js --- week-2/debug/address.js | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/week-2/debug/address.js b/week-2/debug/address.js index 940a6af8..b4c510ea 100644 --- a/week-2/debug/address.js +++ b/week-2/debug/address.js @@ -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}`); From 654c9727e49420d59d8751e97b2bace954e341c6 Mon Sep 17 00:00:00 2001 From: karam Date: Tue, 14 Nov 2023 11:06:59 +0000 Subject: [PATCH 4/5] completed main exercises --- week-2/debug/author.js | 11 +++++++++-- week-2/debug/recipe.js | 8 ++++++-- week-2/implement/contains.js | 10 +++++++++- week-2/implement/contains.test.js | 20 ++++++++++++++++++++ week-2/implement/lookup.js | 26 ++++++++++++++++---------- week-2/implement/lookup.test.js | 5 +++++ week-2/implement/tally.js | 21 +++++++++++++++++++++ week-2/implement/tally.test.js | 30 ++++++++++++++++++++++++++++++ week-2/interpret/invert.js | 17 ++++++++++++++++- 9 files changed, 132 insertions(+), 16 deletions(-) diff --git a/week-2/debug/author.js b/week-2/debug/author.js index 8c212597..6d6ca0f5 100644 --- a/week-2/debug/author.js +++ b/week-2/debug/author.js @@ -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", @@ -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); } diff --git a/week-2/debug/recipe.js b/week-2/debug/recipe.js index 6cbdd22c..995b252a 100644 --- a/week-2/debug/recipe.js +++ b/week-2/debug/recipe.js @@ -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")}`); diff --git a/week-2/implement/contains.js b/week-2/implement/contains.js index cd779308..1307defa 100644 --- a/week-2/implement/contains.js +++ b/week-2/implement/contains.js @@ -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; diff --git a/week-2/implement/contains.test.js b/week-2/implement/contains.test.js index e75984b8..75bd9db2 100644 --- a/week-2/implement/contains.test.js +++ b/week-2/implement/contains.test.js @@ -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); + }); \ No newline at end of file diff --git a/week-2/implement/lookup.js b/week-2/implement/lookup.js index d4677714..61c164e1 100644 --- a/week-2/implement/lookup.js +++ b/week-2/implement/lookup.js @@ -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"); diff --git a/week-2/implement/lookup.test.js b/week-2/implement/lookup.test.js index 8804cb65..d0e53773 100644 --- a/week-2/implement/lookup.test.js +++ b/week-2/implement/lookup.test.js @@ -1,3 +1,5 @@ +const createLookup = require("./lookup"); + /* Create a lookup object of key value pairs from an array of code pairs @@ -29,3 +31,6 @@ It should return: 'CA': 'CAD' } */ +test("converts a single pair of currency codes", () => { + expect(createLookup(["GB", "GBP"])).toEqual({ GB: "GBP" }); +}); \ No newline at end of file diff --git a/week-2/implement/tally.js b/week-2/implement/tally.js index e69de29b..d5cffc30 100644 --- a/week-2/implement/tally.js +++ b/week-2/implement/tally.js @@ -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; \ No newline at end of file diff --git a/week-2/implement/tally.test.js b/week-2/implement/tally.test.js index b473b750..d8ee2049 100644 --- a/week-2/implement/tally.test.js +++ b/week-2/implement/tally.test.js @@ -1,3 +1,5 @@ +const tally = require("./tally"); + /** * tally array * @@ -18,14 +20,42 @@ // When passed an array of items // Then it should return an object containing the count for each unique item +test("returns an object containing the count of each unique item", () => { + const currentOutput = tally(["a"]); + const targetOutput = { a: 1 }; + + expect(currentOutput).toEqual(targetOutput); + }); + // Given an empty array // When passed to tally // Then it should return an empty object +test("if tally is empty returns an empty object", () => { + const currentOutput = tally([]); + const targetOutput = {}; + + expect(currentOutput).toEqual(targetOutput); + }); + // Given an array with duplicate items // When passed to tally // Then it should return counts for each unique item +test("returns an object containing the count of each unique item", () => { + const currentOutput = tally(["a", "a", "a", "b", "square", "square"]); + const targetOutput = { a: 3, b: 1, square: 2 }; + + expect(currentOutput).toEqual(targetOutput); + }); + // Given an invalid input like a string // When passed to tally // Then it should throw an error + +test("returns an object containing the count of each unique item", () => { + const currentOutput = tally("Hello"); + const targetOutput = "Error, you should pass an array of values"; + + expect(currentOutput).toBe(targetOutput); + }); \ No newline at end of file diff --git a/week-2/interpret/invert.js b/week-2/interpret/invert.js index 00d27025..0f477663 100644 --- a/week-2/interpret/invert.js +++ b/week-2/interpret/invert.js @@ -10,18 +10,33 @@ function invert(obj) { const invertedObj = {}; for (const [key, value] of Object.entries(obj)) { - invertedObj.key = value; + invertedObj [value] = key; } return invertedObj; } +console.log(invert({ x: 10, y: 20 })); + // a) What is the current return value when invert is called with { a : 1 } +// { a : 1 } + // b) What is the current return value when invert is called with { a: 1, b: 2 } +// { a: 1, b: 2 } + // c) What is the target return value when invert is called with {a : 1, b: 2} +// { 1 : a, 2 : b } + // c) What does Object.entries return? Why is it needed in this program? +// Object.entries creates an array of the key-value pairs. This allows us to identify in the for loop +// what key and value should be paired. Then we can reverse the pair instead of the complete object? + // d) Explain why the current return value is different from the target output + +// Because the values and keys are turned into arrays in an object. Whereas before, they were just +// key pair values inside of an Object. Also, there positions have now changed. The values are now the keys, +// and the keys are now the values. \ No newline at end of file From 9f0b7ce8a58416ebabddab653232c9b41801ae60 Mon Sep 17 00:00:00 2001 From: karam Date: Wed, 20 Dec 2023 11:14:41 +0000 Subject: [PATCH 5/5] added css and javascript --- week-3/alarmclock/alarmclock.js | 54 ++++++++++++++++++++++++++++++++- week-3/alarmclock/index.html | 2 +- week-3/alarmclock/style.css | 4 +++ 3 files changed, 58 insertions(+), 2 deletions(-) diff --git a/week-3/alarmclock/alarmclock.js b/week-3/alarmclock/alarmclock.js index 6ca81cd3..56f21938 100644 --- a/week-3/alarmclock/alarmclock.js +++ b/week-3/alarmclock/alarmclock.js @@ -1,4 +1,56 @@ -function setAlarm() {} +let timeRemaining = document.getElementById("timeRemaining"); + +function setAlarm() { + let input = document.getElementById("alarmSet").value; + + if (input <= 0) { + alert("Invalid number"); + } + else if(input > 0){ + displayTime(input); + const interval = setInterval(() => { + if (input === 0) { + clearInterval(interval); + playAlarm(); + } + displayTime(input--); + }, 1000); + } + console.log(input); +} + +function displayTime(num) { + let time; + let minutes = Math.floor(num / 60); + let seconds = num % 60; + + if(num >= 0){ + + if (minutes < 10) { + minutesPart = `0${minutes}`; + } else { + minutesPart = minutes; + } + if (seconds < 10) { + secondsPart = `0${seconds}`; + } else { + secondsPart = seconds; + } + time = `${minutesPart}:${secondsPart}`; + timeRemaining.textContent = `Time Remaining: ${time}`; + + } + else if (input < 0) { + time = `00:00`; + timeRemaining.textContent = `Time Remaining: ${time}`; + } +} + + + + + + // DO NOT EDIT BELOW HERE diff --git a/week-3/alarmclock/index.html b/week-3/alarmclock/index.html index 48e2e80d..4a91379d 100644 --- a/week-3/alarmclock/index.html +++ b/week-3/alarmclock/index.html @@ -4,7 +4,7 @@ - Title here + Alarm Clock App
diff --git a/week-3/alarmclock/style.css b/week-3/alarmclock/style.css index 0c72de38..b8570969 100644 --- a/week-3/alarmclock/style.css +++ b/week-3/alarmclock/style.css @@ -1,3 +1,7 @@ +body { + background-color: rgb(247, 136, 85); +} + .centre { position: fixed; top: 50%;