diff --git a/Sprint-2/debug/address.js b/Sprint-2/debug/address.js index 940a6af83..a3217b713 100644 --- a/Sprint-2/debug/address.js +++ b/Sprint-2/debug/address.js @@ -1,5 +1,6 @@ // Predict and explain first... - +// the const is an object, not an array, so [0] in the console log isn't correc +t // This code should log out the houseNumber from the address object // but it isn't working... // Fix anything that isn't working @@ -9,7 +10,7 @@ const address = { street: "Imaginary Road", city: "Manchester", country: "England", - postcode: "XYZ 123", + postcode: "xyz 123", }; -console.log(`My house number is ${address[0]}`); +console.log(`my house number is ${address.houseNumber}`); diff --git a/Sprint-2/debug/author.js b/Sprint-2/debug/author.js index 8c2125977..3b3d1a776 100644 --- a/Sprint-2/debug/author.js +++ b/Sprint-2/debug/author.js @@ -1,5 +1,5 @@ // Predict and explain first... - +// we should take the object and access to the values through dotation. // 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 @@ -11,6 +11,6 @@ const author = { alive: true, }; -for (const value of author) { +for (const value of Object.values(author)) { console.log(value); } diff --git a/Sprint-2/debug/recipe.js b/Sprint-2/debug/recipe.js index 6cbdd22cd..6822a4e1f 100644 --- a/Sprint-2/debug/recipe.js +++ b/Sprint-2/debug/recipe.js @@ -10,6 +10,6 @@ const recipe = { ingredients: ["olive oil", "tomatoes", "salt", "pepper"], }; -console.log(`${recipe.title} serves ${recipe.serves} - ingredients: -${recipe}`); +console.log(`${recipe.title}`); +console.log(`serves: ${recipe.serves}`); +recipe.ingredients.forEach(i => console.log("-"+i)); \ No newline at end of file diff --git a/Sprint-2/implement/contains.js b/Sprint-2/implement/contains.js index cd779308a..9d23d92cf 100644 --- a/Sprint-2/implement/contains.js +++ b/Sprint-2/implement/contains.js @@ -1,3 +1,23 @@ -function contains() {} +function contains(obj, prop) { + + if (typeof obj !== "object" && obj === null ) { //typeof to check if the key is an object + return false; + } else if (Array.isArray(obj)) { //array.isarray to check that the obj is an array, so we throw an error message + throw new Error ("invalid input"); + } + + return prop in obj; //in method to check if a property is in the object + +} +console.log(contains({ a: 1, b: 2 }, "a")); +console.log(contains({ a: 1, b: 2 }, "c")); +console.log(contains({})); +console.log(contains({undefined},"b")); + +try { + console.log(contains([1, 2, 3], "3")); +} catch (E) { + console.log(E.message); +} module.exports = contains; diff --git a/Sprint-2/implement/lookup.js b/Sprint-2/implement/lookup.js index a6746e07f..5573ac36c 100644 --- a/Sprint-2/implement/lookup.js +++ b/Sprint-2/implement/lookup.js @@ -1,5 +1,19 @@ -function createLookup() { - // implementation here -} +function createLookup(codePairs) { + + if (!Array.isArray(codePairs)) { + return null; + } + + return Object.fromEntries(codePairs); //convert the code pairs arrays into object + +} + + const countryCurrency = [ + ["US", "USD"], + ["CA", "CAD"], + ]; + +const result = createLookup(countryCurrency); +console.log(result); module.exports = createLookup; diff --git a/Sprint-2/implement/querystring.js b/Sprint-2/implement/querystring.js index 45ec4e5f3..b1ccb095f 100644 --- a/Sprint-2/implement/querystring.js +++ b/Sprint-2/implement/querystring.js @@ -6,11 +6,12 @@ function parseQueryString(queryString) { const keyValuePairs = queryString.split("&"); for (const pair of keyValuePairs) { - const [key, value] = pair.split("="); + const [key, ...rest] = pair.split("="); //split the first = from the key + const value = rest.join("="); //join the value queryParams[key] = value; - } + } return queryParams; -} +} module.exports = parseQueryString; diff --git a/Sprint-2/implement/querystring.test.js b/Sprint-2/implement/querystring.test.js index 3e218b789..2f120598f 100644 --- a/Sprint-2/implement/querystring.test.js +++ b/Sprint-2/implement/querystring.test.js @@ -10,3 +10,15 @@ test("parses querystring values containing =", () => { "equation": "x=y+1", }); }); + +test("multiple key-value pairs", () => { + expect(parseQueryString("a=1&b=2")).toEqual({ a: "1", b: "2" }); +}); + +test("only = character", () => { + expect(parseQueryString("=")).toEqual({ "": "" }); +}); + +test("empty key with value", () => { + expect(parseQueryString("=value")).toEqual({ "": "value" }); +}); diff --git a/Sprint-2/implement/tally.js b/Sprint-2/implement/tally.js index f47321812..7142e2afa 100644 --- a/Sprint-2/implement/tally.js +++ b/Sprint-2/implement/tally.js @@ -1,3 +1,27 @@ -function tally() {} +function tally(arr) { + + if (!Array.isArray(arr)) { + throw new TypeError("Input must be an array"); + } + + const counts = {}; //store the result as an object + + for (const items of arr) { //for... of loop to count the items + if (counts[items]) { + counts[items] += 1; + } else { + counts[items] = 1; + } + } + + return counts; + +} + +console.log(tally(["a","a","c"])); +console.log(tally(["a", "5", "5"])); +console.log(tally([])); +console.log(tally("hello world")); + module.exports = tally; diff --git a/Sprint-2/interpret/invert.js b/Sprint-2/interpret/invert.js index bb353fb1f..a9bd4ae06 100644 --- a/Sprint-2/interpret/invert.js +++ b/Sprint-2/interpret/invert.js @@ -10,20 +10,34 @@ function invert(obj) { const invertedObj = {}; for (const [key, value] of Object.entries(obj)) { - invertedObj.key = value; + invertedObj[value] = key; } return invertedObj; } -// a) What is the current return value when invert is called with { a : 1 } +// a) What is the current return value when invert is called with { a : 1 } = { key : 1} -// b) What is the current return value when invert is called with { a: 1, b: 2 } +// b) What is the current return value when invert is called with { a: 1, b: 2 } = { key : 2} -// c) What is the target return value when invert is called with {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? +// c) What does Object.entries return? Why is it needed in this program? = returns an array of key, value pairs. It's important bc we can loop both, key and values. -// d) Explain why the current return value is different from the target output +// d) Explain why the current return value is different from the target output = because invertedObj.key is just setting a property called "key" +// we need to use the [] to access to the variable. + +// e) Fix the implementation of invert (and write tests to prove it's fixed!) + +test("inverts object with numeric strings value", () => { + expect(invert({ a: 1, b: 2 })).toBe({ 1: a, 2: b }); +}); + +test("inverts object with strings value", () => { + expect(invert({ a: "hello", b: "world" })).toBe({ "hello": a, "world": b }); +}); + +test("inverts object with numeric strings value and literal strings value", () => { + expect(invert({ a: "hello", b: 5 })).toBe({ hello: a, 5 : b }); +}); -// e) Fix the implementation of invert (and write tests to prove it's fixed!) diff --git a/Sprint-3/alarmclock/alarmclock.js b/Sprint-3/alarmclock/alarmclock.js index 6ca81cd3b..bfb064cfb 100644 --- a/Sprint-3/alarmclock/alarmclock.js +++ b/Sprint-3/alarmclock/alarmclock.js @@ -1,5 +1,45 @@ -function setAlarm() {} +function setAlarm() { + //get elements from the html file + const input = document.querySelector("#alarmSet"); + const heading = document.querySelector("#timeRemaining"); + const pause = document.querySelector("#pause"); + let totalSeconds = parseInt(input.value, 10); //converting the input into number values (input from html file are always strings) + + if (isNaN(totalSeconds) || totalSeconds <= 0) { //check if is not a number and if the number is 0 or less than 0 to return a message for invalid input + heading.innerText = "Please enter a valid number of seconds"; + return; + } + + //helper function + function formatTime(seconds) { + const minutes = Math.floor(seconds / 60); + const secs = seconds % 60; + return `${minutes.toString().padStart(2, "0")}:${secs + .toString() + .padStart(2, "0")}`; + } + + heading.innerText = `Time Remaining: ${formatTime(totalSeconds)}`; //updating heading + + const timerId = setInterval(() => { //countdown + totalSeconds--; //decrements operator + + heading.innerText = `Time Remaining: ${formatTime(totalSeconds)}`; //updating heading every second after the countdown runs + + if (totalSeconds <= 0) { + clearInterval(timerId); + if (typeof playAlarm === "function") { //only runs if playAlarm is a function (safe code) + playAlarm(); + } + } + }, 1000); + + + + + +} // DO NOT EDIT BELOW HERE var audio = new Audio("alarmsound.mp3"); @@ -12,6 +52,10 @@ function setup() { document.getElementById("stop").addEventListener("click", () => { pauseAlarm(); }); + + document.getElementById("pause").addEventListener("click", () => { + pauseCountDown(); + }); } function playAlarm() { @@ -22,4 +66,5 @@ function pauseAlarm() { audio.pause(); } + window.onload = setup; diff --git a/Sprint-3/alarmclock/index.html b/Sprint-3/alarmclock/index.html index 48e2e80d9..ecdc8c503 100644 --- a/Sprint-3/alarmclock/index.html +++ b/Sprint-3/alarmclock/index.html @@ -4,7 +4,7 @@ -
You have 200 characters remaining
+