diff --git a/1-exercises/A-accessing-values/exercise1.js b/1-exercises/A-accessing-values/exercise1.js index 67416c69..dcf5a5ce 100644 --- a/1-exercises/A-accessing-values/exercise1.js +++ b/1-exercises/A-accessing-values/exercise1.js @@ -8,7 +8,7 @@ let dog = { breed: "Dalmatian", name: "Spot", isHungry: true, - happiness: 6 + happiness: 6, }; /* @@ -16,8 +16,8 @@ let dog = { Log the name and breed of this dog using dot notation. */ -let dogName; // complete the code -let dogBreed; // complete the code +let dogName = dog.name; // complete the code +let dogBreed = dog.breed; // complete the code console.log(`${dogName} is a ${dogBreed}`); @@ -25,4 +25,4 @@ console.log(`${dogName} is a ${dogBreed}`); Spot is a Dalmatian -*/ \ No newline at end of file +*/ diff --git a/1-exercises/A-accessing-values/exercise2.js b/1-exercises/A-accessing-values/exercise2.js index 5b523ace..cb3e9311 100644 --- a/1-exercises/A-accessing-values/exercise2.js +++ b/1-exercises/A-accessing-values/exercise2.js @@ -7,7 +7,7 @@ let capitalCities = { UnitedKingdom: "London", China: "Beijing", - Peru: "Lima" + Peru: "Lima", }; /* @@ -17,7 +17,7 @@ let capitalCities = { */ let myCountry = "UnitedKingdom"; -let myCapitalCity; // complete the code +let myCapitalCity = capitalCities["UnitedKingdom"]; // complete the code console.log(myCapitalCity); @@ -25,4 +25,4 @@ console.log(myCapitalCity); London -*/ \ No newline at end of file +*/ diff --git a/1-exercises/A-accessing-values/exercise3.js b/1-exercises/A-accessing-values/exercise3.js index 2e160dd5..a6d7f097 100644 --- a/1-exercises/A-accessing-values/exercise3.js +++ b/1-exercises/A-accessing-values/exercise3.js @@ -19,14 +19,16 @@ let basketballTeam = { - sorts the top players in alphabetical order - console.logs the name of each player on a new line */ - +topGuyList = basketballTeam.topPlayers.sort(); +for (let i = 0; i < topGuyList.length; i++) { + console.log(topGuyList[i]); +} // write code here - /* EXPECTED RESULT Dennis Rodman Michael Jordan Scottie Pippen -*/ \ No newline at end of file +*/ diff --git a/1-exercises/C-undefined-properties/exercise.js b/1-exercises/C-undefined-properties/exercise.js index 8b00f6ce..154ad6d1 100644 --- a/1-exercises/C-undefined-properties/exercise.js +++ b/1-exercises/C-undefined-properties/exercise.js @@ -15,6 +15,7 @@ let car = { }; console.log(car["colour"]); +//car does not have an attribute of colour - I would spell it color to match the other American spellings used // Example 2 function sayHelloToUser(user) { @@ -22,17 +23,20 @@ function sayHelloToUser(user) { } let user = { - name: "Mira" + name: "Mira", }; +//No attribute of firstName + sayHelloToUser(user); // Example 3 let myPet = { animal: "Cat", - getName: function() { + getName: function () { "My pet's name is Fluffy"; }, }; console.log(myPet.getName()); +//The return key word has not been added to the method getName diff --git a/1-exercises/D-object-methods/exercise.js b/1-exercises/D-object-methods/exercise.js index 0b57f2e1..fa03d48e 100644 --- a/1-exercises/D-object-methods/exercise.js +++ b/1-exercises/D-object-methods/exercise.js @@ -9,7 +9,10 @@ let student = { // write code here -} + getName: function (name) { + console.log(`Student name: ${name}`); + }, +}; student.getName("Daniel"); @@ -17,4 +20,4 @@ student.getName("Daniel"); Student name: Daniel -*/ \ No newline at end of file +*/ diff --git a/2-mandatory/1-recipes.js b/2-mandatory/1-recipes.js index 6243fa9c..165c86bd 100644 --- a/2-mandatory/1-recipes.js +++ b/2-mandatory/1-recipes.js @@ -22,4 +22,20 @@ You should write and log at least 5 recipes */ -// write code here \ No newline at end of file +// write code here +let banoffee = { + title:"Banoffee", + servings: 7 , + ingredients: ["bananas","biscuits","butter","condensed milk","sugar","cream"] + + +} + +console.log(banoffee.title) +console.log("Serves: " + banoffee.servings) +console.log("Ingredients:") + +for (let i = 0; i { NG: "NGN", MX: "MXN", }); -}); \ No newline at end of file +}); diff --git a/2-mandatory/3-shopping-list.js b/2-mandatory/3-shopping-list.js index d25cb366..134643b2 100644 --- a/2-mandatory/3-shopping-list.js +++ b/2-mandatory/3-shopping-list.js @@ -19,9 +19,47 @@ let pantry = { }; function createShoppingList(recipe) { - // write code here + let missingIngredients = recipe.ingredients.filter(function (ingredient) { + return ( + !pantry.fridgeContents.includes(ingredient) && + !pantry.cupboardContents.includes(ingredient) + ); + }); + return { + name: recipe.name, + items: missingIngredients, + }; +} + +/* +function createShoppingList(recipe) { + let missingIngredients = recipe.ingredients.filter(function(ingredient) { + return !pantry.fridgeContents.includes(ingredient) && !pantry.cupboardContents.includes(ingredient); + }); + return { + name: recipe.name, + items: missingIngredients + }; } +//Test this on JSFiddle + +let pizzaRecipe = { + name: "Pizza", + ingredients: ["dough", "cheese", "tomato sauce"] +}; + +let shoppingList = createShoppingList(pizzaRecipe); +console.log(shoppingList); + +// Output: { name: "Pizza", items: ["dough", "cheese"] } + + + + + +*/ + /* ======= TESTS - DO NOT MODIFY ===== - To run the tests for this exercise, run `npm test -- --testPathPattern 3-shopping-list.js` - To run all exercises/tests in the mandatory folder, run `npm test` @@ -43,11 +81,18 @@ test("createShoppingList works for pancakes recipe", () => { test("createShoppingList works for margherita pizza recipe", () => { let recipe2 = { name: "margherita pizza", - ingredients: ["flour", "salt", "yeast", "tinned tomatoes", "oregano", "mozarella"], + ingredients: [ + "flour", + "salt", + "yeast", + "tinned tomatoes", + "oregano", + "mozarella", + ], }; expect(createShoppingList(recipe2)).toEqual({ name: "margherita pizza", - items: ["flour", "yeast", "mozarella"] + items: ["flour", "yeast", "mozarella"], }); -}); \ No newline at end of file +}); diff --git a/2-mandatory/4-restaurant.js b/2-mandatory/4-restaurant.js index d7b81eea..46542b5a 100644 --- a/2-mandatory/4-restaurant.js +++ b/2-mandatory/4-restaurant.js @@ -17,10 +17,30 @@ const MENU = { burger: 6.5, falafel: 7.25, -}; + + } +; let cashRegister = { // write code here + orderBurger: function(balance){ + if(balance<6.5){ + return "You do not have enough money" + } + else if (balance >= 6.5) { + let newBalance = balance - 6.5 + return newBalance + } +}, + + orderFalafel: function(balance){ + if(balance<7.25){ + return "You do not have enough money" + } + else if (balance >= 7.25) { + let newBalance = balance - 7.25 + return newBalance + } } /* ======= TESTS - DO NOT MODIFY ===== diff --git a/2-mandatory/5-writing-tests.js b/2-mandatory/5-writing-tests.js index 1443608b..def9555a 100644 --- a/2-mandatory/5-writing-tests.js +++ b/2-mandatory/5-writing-tests.js @@ -35,7 +35,7 @@ function convertScoreToGrade(score) { passes. */ test("a score of 83 is grade A", () => { - expect(convertScoreToGrade(83), "Z"); + expect(convertScoreToGrade(83), "A"); }); /* @@ -49,27 +49,46 @@ test.skip("a score of 71 is grade B", () => { /* Write a test that checks a score of 68 is grade C */ - +test("a score of 68 is grade C", () => { + expect(convertScoreToGrade(68), "Z"); +}); /* Write a test that checks a score of 55 is grade D */ +test("a score of 55 is grade D", () => { + expect(convertScoreToGrade(55), ""); +}); /* Write a test that checks a score of 68 is grade C */ +test("a score of 68 is grade C", () => { + expect(convertScoreToGrade(68), "C"); +}); /* Write a test that checks a score of 55 is grade D */ - +test("a score of 55 is grade D", () => { + expect(convertScoreToGrade(55), "D"); +}); /* Write a test that checks a score of 49 is grade E */ +test("a score of 49 is grade E", () => { + expect(convertScoreToGrade(55), ""); +}); /* Write a test that checks a score of 30 is grade E */ +test("a score of 30 is grade E", () => { + expect(convertScoreToGrade(30), "E"); +}); /* Write a test that checks a score of 70 is grade B */ +test("a score of 70 is grade B", () => { + expect(convertScoreToGrade(70), "D"); +}); diff --git a/3-extra/1-count-words.js b/3-extra/1-count-words.js index 9d347ae5..d4bfa0ed 100644 --- a/3-extra/1-count-words.js +++ b/3-extra/1-count-words.js @@ -23,12 +23,43 @@ - Setting values on an object */ +<<<<<<< HEAD +function countWords(str) { + let array = str.split(" "); + const counts = {}; + for (let i = 0; i < array.length; i++) { + const word = array[i]; + if (counts[word]) { + counts[word]++; + } else { + counts[word] = 1; + } + } + return counts; +======= function countWords(string) { + // write code here + + let strLower = string.toLowerCase(); const wordCount = {}; - // write code here + // split the string into an array of words + const words = strLower.split(" "); + + // loop through each word + words.forEach((word) => { + // check if the word already exists in the object + if (wordCount[word]) { + // if it does, increment the value of that key + wordCount[word]++; + } else { + // if it doesn't, add it to the object with a value of 1 + wordCount[word] = 1; + } + }); return wordCount; +>>>>>>> 26cab01f55b1033f71c12f38340eba5bac196f45 } /* ======= TESTS - DO NOT MODIFY ===== @@ -46,9 +77,13 @@ test("Code works for a small string", () => { }); test("A string with, some punctuation", () => { - expect(countWords("A string with, some punctuation")).toEqual( - { A: 1, string: 1, "with,": 1, some: 1, punctuation: 1 } - ); + expect(countWords("A string with, some punctuation")).toEqual({ + A: 1, + string: 1, + "with,": 1, + some: 1, + punctuation: 1, + }); }); test("Empty string", () => { @@ -56,7 +91,11 @@ test("Empty string", () => { }); test("Example task string", () => { - expect(countWords("you're braver than you believe, stronger than you seem, and smarter than you think")).toEqual({ + expect( + countWords( + "you're braver than you believe, stronger than you seem, and smarter than you think" + ) + ).toEqual({ "you're": 1, and: 1, "believe,": 1,