diff --git a/Sprint-2/debug/address.js b/Sprint-2/debug/address.js index 940a6af83..b99ea43cf 100644 --- a/Sprint-2/debug/address.js +++ b/Sprint-2/debug/address.js @@ -1,4 +1,7 @@ // Predict and explain first... +//Prediction: The will be an error at ${address[0]}. +//Explanation: calling an object using the index as in array. Instead we should use the dot method(.address) or the brackets["address"]. + // This code should log out the houseNumber from the address object // but it isn't working... @@ -12,4 +15,4 @@ const address = { 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..3a540064c 100644 --- a/Sprint-2/debug/author.js +++ b/Sprint-2/debug/author.js @@ -1,4 +1,6 @@ // Predict and explain first... +// Prediction: Going to shows an error in the for loop. +// Explanation: we should have used the for .. in method (The for...in statement iterates over all enumerable string properties of an object (ignoring properties keyed by symbols), including inherited enumerable properties.) // 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 +13,6 @@ const author = { alive: true, }; -for (const value of author) { - console.log(value); +for (const value in author){ + console.log(`${value}: ${author[value]}`); } diff --git a/Sprint-2/debug/recipe.js b/Sprint-2/debug/recipe.js index 6cbdd22cd..bebef6316 100644 --- a/Sprint-2/debug/recipe.js +++ b/Sprint-2/debug/recipe.js @@ -1,4 +1,6 @@ // Predict and explain first... +// Prediction: An error in logging out the ingredients. +// Explanation: Because of the way to get the value or an element from an array should used the dot notation for example and reach each element using its index. // This program should log out the title, how many it serves and the ingredients. // Each ingredient should be logged on a new line @@ -12,4 +14,7 @@ const recipe = { console.log(`${recipe.title} serves ${recipe.serves} ingredients: -${recipe}`); +${recipe.ingredients[0]} +${recipe.ingredients[1]} +${recipe.ingredients[2]} +${recipe.ingredients[3]}`); diff --git a/Sprint-2/implement/contains.js b/Sprint-2/implement/contains.js index cd779308a..2716d7051 100644 --- a/Sprint-2/implement/contains.js +++ b/Sprint-2/implement/contains.js @@ -1,3 +1,9 @@ -function contains() {} +function contains(input, propertyName) { + if (input && typeof input === 'object' && !Array.isArray(input)) { + return input.hasOwnProperty(propertyName); + } + return false; + +} module.exports = contains; diff --git a/Sprint-2/implement/contains.test.js b/Sprint-2/implement/contains.test.js index 326bdb1f2..3f7778611 100644 --- a/Sprint-2/implement/contains.test.js +++ b/Sprint-2/implement/contains.test.js @@ -17,19 +17,63 @@ 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("given an object contains a property name, returns true", function (){ + const input = {a: 1, b: 2}; + const propertyName = 'a'; + const currentOutput = contains(input, propertyName); + const targetOutput = true; + + expect(currentOutput).toEqual(targetOutput); +}); + // Given an empty object // When passed to contains // Then it should return false -test.todo("contains on empty object returns false"); + +test("given an empty object, returns false", function (){ + const input = {}; + const propertyName = 'a'; + const currentOutput = contains(input, propertyName); + const targetOutput = false; + + expect(currentOutput).toEqual(targetOutput); +}); // Given an object with properties // When passed to contains with an existing property name // Then it should return true +test("given an object contains a property name, returns true", function (){ + const input = {a: 1, b: 2}; + const propertyName = 'a'; + const currentOutput = contains(input, propertyName); + const targetOutput = true; + + expect(currentOutput).toEqual(targetOutput); +}); + // Given an object with properties // When passed to contains with a non-existent property name // Then it should return false +test("given an object does not contain a property name, returns false", function (){ + const input = {a: 1, b: 2}; + const propertyName = 'c'; + const currentOutput = contains(input, propertyName); + const targetOutput = false; + + expect(currentOutput).toEqual(targetOutput); +}); + // Given invalid parameters like an array // When passed to contains // Then it should return false or throw an error + +test("given invalid parameters like an array, returns false", function (){ + const input = [1, 2, 3]; + const propertyName = 'a'; + const currentOutput = contains(input, propertyName); + const targetOutput = false; + + expect(currentOutput).toEqual(targetOutput); +}); \ No newline at end of file diff --git a/Sprint-3/quote-generator/index.html b/Sprint-3/quote-generator/index.html index 30b434bcf..cb9f3e860 100644 --- a/Sprint-3/quote-generator/index.html +++ b/Sprint-3/quote-generator/index.html @@ -3,13 +3,17 @@ - Title here - + Quote Generator App</A> +
+

hello there

+ +
+ diff --git a/Sprint-3/quote-generator/quotes.js b/Sprint-3/quote-generator/quotes.js index 4a4d04b72..66c56caf8 100644 --- a/Sprint-3/quote-generator/quotes.js +++ b/Sprint-3/quote-generator/quotes.js @@ -1,3 +1,19 @@ +const quoteElem = document.getElementById("quote"); +const authorElem = document.getElementById("author"); +const button = document.getElementById("new-quote"); + +function showNewQuote() { + const random = pickFromArray(quotes); + quoteElem.textContent = random.quote; + authorElem.textContent = random.author; +} + +window.addEventListener("DOMContentLoaded", () => { + showNewQuote(); + + button.addEventListener("click", showNewQuote); +}); + // DO NOT EDIT BELOW HERE // pickFromArray is a function which will return one item, at