diff --git a/Sprint-2/debug/address.js b/Sprint-2/debug/address.js index 940a6af83..a0a90be91 100644 --- a/Sprint-2/debug/address.js +++ b/Sprint-2/debug/address.js @@ -1,5 +1,7 @@ // Predict and explain first... +//An address is an object. Access its details using names (like 'houseNumber'), not by counting them with numbers + // This code should log out the houseNumber from the address object // but it isn't working... // Fix anything that isn't working @@ -12,4 +14,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..b1aee82e6 100644 --- a/Sprint-2/debug/author.js +++ b/Sprint-2/debug/author.js @@ -10,7 +10,80 @@ const author = { age: 40, alive: true, }; +const onlyValues = Object.values(author); -for (const value of author) { +for (const value of onlyValues) { console.log(value); } + +const studentScores = { + math: 85, + science: 92, + history: 78, + art: 95, +}; + +let totalSum = 0; +const sumArray = Object.values(studentScores); + +for (const value of sumArray) { + totalSum += value; +} +console.log(totalSum); + +// Goal: Calculate the sum of all the student's scores. +// You need to get an array of just the numbers (values) first. +// How would you get an array like: [85, 92, 78, 95] from studentScores? + +// Exercise 2: List All Ingredients + +// You have a recipe object, and you want to list out all the ingredients. + +// Goal: Print a list of all ingredient names (keys) from the 'ingredients' object. +// You need to get an array like: ["flour", "milk", "eggs", "sugar", "bakingPowder"] +// from the 'ingredients' object. + +// need to make object interable = array and get the key.values ofthe object + +const recipe = { + title: "Pancakes", + servings: 4, + ingredients: { + flour: "1 cup", + milk: "1 cup", + eggs: "2 large", + sugar: "2 tbsp", + bakingPowder: "2 tsp", + }, + instructions: "Mix ingredients, cook on griddle.", +}; + +const ingredientsList = Object.keys(recipe.ingredients); + +for (const value of ingredientsList) { + console.log(value); +} + +// Exercise 3: Format User Preferences + +// A user's preferences are stored in an object. You want to display each preference as "Key: Value". + +const userSettings = { + theme: "dark", + notifications: true, + language: "en-US", + autoplayVideo: false, +}; + +let userPreferences = Object.entries(userSettings); + +for (const values of userPreferences) { + console.log(values); //console.log(`${key}: ${value}`); //console.log(`${pair[0]}: ${pair[1]}`); +} + +// Goal: Log each setting in the format "Key: Value". +// Example output: +// "theme: dark" +// "notifications: true" +// ... +// This requires working with both the key and the value for each item. diff --git a/Sprint-2/debug/recipe.js b/Sprint-2/debug/recipe.js index 6cbdd22cd..f3cdb4139 100644 --- a/Sprint-2/debug/recipe.js +++ b/Sprint-2/debug/recipe.js @@ -1,15 +1,23 @@ // Predict and explain first... - +// the code try to print a array in a single line of ingredients and also has +// syntax erros // This program should log out the title, how many it serves and the ingredients. +// I need to get the values but firnt need to make the object interable=use Object.value // Each ingredient should be logged on a new line // How can you fix it? const recipe = { - title: "bruschetta", - serves: 2, - ingredients: ["olive oil", "tomatoes", "salt", "pepper"], +title: "bruschetta", +serves: 2, +ingredients: ["olive oil", "tomatoes", "salt", "pepper"], }; -console.log(`${recipe.title} serves ${recipe.serves} - ingredients: -${recipe}`); +let menuList = Object.values(recipe.ingredients); + +console.log (`${recipe.title}`); + console.log (`serves ${recipe.serves}`); + + +for (const ingredientName of menuList) { +console.log(ingredientName); +} \ No newline at end of file