From f9a3d9d5340e6f43ef703c175d54dcf234632d5a Mon Sep 17 00:00:00 2001 From: nfarah22 <61600465+nfarah22@users.noreply.github.com> Date: Wed, 22 Mar 2023 00:13:13 +0000 Subject: [PATCH] completed js2.1 --- 1-exercises/A-accessing-values/exercise1.js | 4 +- 1-exercises/A-accessing-values/exercise2.js | 2 +- 1-exercises/A-accessing-values/exercise3.js | 8 ++ 1-exercises/B-setting-values/exercise1.js | 6 ++ 1-exercises/B-setting-values/exercise2.js | 17 ++-- .../C-undefined-properties/exercise.js | 2 +- 1-exercises/D-object-methods/exercise.js | 5 + 2-mandatory/1-recipes.js | 60 +++++++++++- 2-mandatory/2-currency-code-lookup.js | 8 +- 2-mandatory/3-shopping-list.js | 19 +++- 2-mandatory/4-restaurant.js | 23 ++++- 2-mandatory/5-writing-tests.js | 51 ++++++---- 2-mandatory/6-writing-tests-advanced.js | 96 +++++++++++++------ 13 files changed, 229 insertions(+), 72 deletions(-) diff --git a/1-exercises/A-accessing-values/exercise1.js b/1-exercises/A-accessing-values/exercise1.js index 67416c69..605b7fcb 100644 --- a/1-exercises/A-accessing-values/exercise1.js +++ b/1-exercises/A-accessing-values/exercise1.js @@ -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}`); diff --git a/1-exercises/A-accessing-values/exercise2.js b/1-exercises/A-accessing-values/exercise2.js index 5b523ace..9f63ef10 100644 --- a/1-exercises/A-accessing-values/exercise2.js +++ b/1-exercises/A-accessing-values/exercise2.js @@ -17,7 +17,7 @@ let capitalCities = { */ let myCountry = "UnitedKingdom"; -let myCapitalCity; // complete the code +let myCapitalCity= capitalCities["UnitedKingdom"]; // complete the code console.log(myCapitalCity); diff --git a/1-exercises/A-accessing-values/exercise3.js b/1-exercises/A-accessing-values/exercise3.js index 2e160dd5..b871231d 100644 --- a/1-exercises/A-accessing-values/exercise3.js +++ b/1-exercises/A-accessing-values/exercise3.js @@ -21,8 +21,16 @@ let basketballTeam = { */ // write code here +function getPlayers(){ +let players=basketballTeam.topPlayers ; +players.sort(); +for (let i = 0;i < basketballTeam.topPlayers.length;i++){ + console.log(players[i]+ "\n") +} +} +getPlayers() /* EXPECTED RESULT Dennis Rodman diff --git a/1-exercises/B-setting-values/exercise1.js b/1-exercises/B-setting-values/exercise1.js index 7d0b05c5..e82d718c 100644 --- a/1-exercises/B-setting-values/exercise1.js +++ b/1-exercises/B-setting-values/exercise1.js @@ -23,6 +23,12 @@ let capitalCities = { */ // write code here +capitalCities.UnitedKingdom.population=8980000 +capitalCities.China.population=21500000 +capitalCities.peru = { + name: "Lima", + population:9750000, +} console.log(capitalCities); diff --git a/1-exercises/B-setting-values/exercise2.js b/1-exercises/B-setting-values/exercise2.js index 59fb7c1e..9af1afa5 100644 --- a/1-exercises/B-setting-values/exercise2.js +++ b/1-exercises/B-setting-values/exercise2.js @@ -7,15 +7,14 @@ let student = { name: "Reshma Saujani", examScore: 65, hasPassed: false -}; - +} /* Using bracket notation - Add a property to the student object for attendance - Set the value of attendance to 90 */ -// write code here +student["attendance"] = 90; /* - Write an "if" statement that changes the value of hasPassed to true @@ -24,10 +23,14 @@ let student = { exam score is above 60. - Use bracket notation to change the value of hasPassed */ - -// write code here - -console.log(student); +function passed(student){ + + if (student["attendance"] >= 90 && student["examScore"] > 60){ + student["hasPassed"]= true + } + return student ; +} +console.log(passed(student)); /* EXPECTED RESULT diff --git a/1-exercises/C-undefined-properties/exercise.js b/1-exercises/C-undefined-properties/exercise.js index 8b00f6ce..2a70c105 100644 --- a/1-exercises/C-undefined-properties/exercise.js +++ b/1-exercises/C-undefined-properties/exercise.js @@ -14,7 +14,7 @@ let car = { yearsOld: 8, }; -console.log(car["colour"]); +console.log(car["colour"]); // the key value colour is undefined because // Example 2 function sayHelloToUser(user) { diff --git a/1-exercises/D-object-methods/exercise.js b/1-exercises/D-object-methods/exercise.js index 0b57f2e1..caa661f7 100644 --- a/1-exercises/D-object-methods/exercise.js +++ b/1-exercises/D-object-methods/exercise.js @@ -8,6 +8,11 @@ */ let student = { + getName: function(name){ + console.log(`student name: ${name}`) + } + + // write code here } diff --git a/2-mandatory/1-recipes.js b/2-mandatory/1-recipes.js index 6243fa9c..e4f09a9a 100644 --- a/2-mandatory/1-recipes.js +++ b/2-mandatory/1-recipes.js @@ -22,4 +22,62 @@ You should write and log at least 5 recipes */ -// write code here \ No newline at end of file +// write code here +/*function setcreatNewRecipe(titlePram,servingsPram,ingrediantsPram){ + return { + title:titlePram, + servings:servingsPram, + ingredients:ingrediantsPram + } + } */ + +let recipe = [ { + title: "English Breakfast", + servings:1 , + ingredients:["onions","beans","eggs","hashbrowns"] +}, +{ + title:"Tuna Pasta", + servings:2 , + ingredients:["onions","tuna","pasta",] +}, + +{ + title:"chicken pasta", + servings:4, + ingredients:["chicken","onions","pasta","bell peppers"] +}, +{ title:"chicken rice", + servings:4, + ingredients:["chicken","rice","onions","spices"] + +}, +{ + title:"fish and chips", + servings:2, + ingredients:["fish","potatoes","oil"] +}] +function getRecipe(){ + + for(let i = 0 ;i { let recipe2 = { name: "margherita pizza", ingredients: ["flour", "salt", "yeast", "tinned tomatoes", "oregano", "mozarella"], - }; + } expect(createShoppingList(recipe2)).toEqual({ name: "margherita pizza", items: ["flour", "yeast", "mozarella"] }); -}); \ No newline at end of file +}); \ No newline at end of file diff --git a/2-mandatory/4-restaurant.js b/2-mandatory/4-restaurant.js index d7b81eea..f359018a 100644 --- a/2-mandatory/4-restaurant.js +++ b/2-mandatory/4-restaurant.js @@ -20,14 +20,27 @@ const MENU = { }; let cashRegister = { - // write code here + orderBurger: function (balance){ + if ( balance >=MENU.burger ){ + balance -=MENU.burger ; + } + return balance; + } , + orderFalafel: function (balance){ + if(balance >= MENU.falafel){ + balance-=MENU.falafel + } + return balance + } + } +console.log(cashRegister.orderFalafel(10)); -/* ======= TESTS - DO NOT MODIFY ===== +/*======= TESTS - DO NOT MODIFY ===== - To run the tests for this exercise, run `npm test -- --testPathPattern 4-restaurant.js` - To run all exercises/tests in the mandatory folder, run `npm test` -- (Reminder: You must have run `npm install` one time before this will work!) -*/ +- (Reminder: You must have run `npm install` one time before this will work!) */ + test("orderBurger subtracts 6.5 from balance", () => { let balance = 6.5; @@ -47,4 +60,4 @@ test("orderBurger will not subtract from balance if balance is too low", () => { test("orderFalafel will not subtract from balance if balance is too low", () => { let balance = 7.24; expect(cashRegister.orderFalafel(balance)).toEqual(7.24); -}); +}); diff --git a/2-mandatory/5-writing-tests.js b/2-mandatory/5-writing-tests.js index 1443608b..38c85f10 100644 --- a/2-mandatory/5-writing-tests.js +++ b/2-mandatory/5-writing-tests.js @@ -35,7 +35,8 @@ function convertScoreToGrade(score) { passes. */ test("a score of 83 is grade A", () => { - expect(convertScoreToGrade(83), "Z"); + const score = 83; + expect(convertScoreToGrade(score)).toEqual("A"); }); /* @@ -43,33 +44,43 @@ test("a score of 83 is grade A", () => { write a matching test */ -test.skip("a score of 71 is grade B", () => { - /* Remove the .skip above, then write the test body. */ -}); -/* - Write a test that checks a score of 68 is grade C -*/ +test("a score of 71 is grade B", () => { + const score = 71; + expect(convertScoreToGrade(score)).toEqual("B") +});/* Remove the .skip above, then write the test body. */ -/* - Write a test that checks a score of 55 is grade D -*/ -/* - Write a test that checks a score of 68 is grade C +test("a score of 68 is grade C", () => { + const score = 68; + expect(convertScoreToGrade(score)).toEqual("C") +});/*Write a test that checks a score of 68 is grade C */ -/* - Write a test that checks a score of 55 is grade D + + test("a score of 55 is grade C", () => { + const score = 55; + expect(convertScoreToGrade(score)).toEqual("D") +}); /*Write a test that checks a score of 55 is grade D */ -/* - Write a test that checks a score of 49 is grade E + + test("a score of 49 is grade E", () => { + const score = 49; + expect(convertScoreToGrade(score)).toEqual("E") +}); + /* Write a test that checks a score of 49 is grade E */ -/* - Write a test that checks a score of 30 is grade E + +test("a score of 55 is grade E", () => { + const score = 30; + expect(convertScoreToGrade(score)).toEqual("E") +}); /*Write a test that checks a score of 30 is grade E */ -/* - Write a test that checks a score of 70 is grade B +test("a score of 70 is grade B", () => { + const score = 70; + expect(convertScoreToGrade(score)).toEqual("B") +}); + /*Write a test that checks a score of 70 is grade B */ diff --git a/2-mandatory/6-writing-tests-advanced.js b/2-mandatory/6-writing-tests-advanced.js index 8d227e27..6e9562aa 100644 --- a/2-mandatory/6-writing-tests-advanced.js +++ b/2-mandatory/6-writing-tests-advanced.js @@ -10,10 +10,10 @@ trainee has completed. */ -function convertScoreToGrade() { +function convertScoreToGrade(score) { let grade = null; - if (score >= 80) { + if (score>= 80) { grade = "A"; } else if (score >= 70) { grade = "B"; @@ -42,50 +42,86 @@ function formatCourseworkResult(trainee) { return `${traineeName}'s coursework was marked as grade ${traineeGrade}.`; } + + + /* ======= TESTS - FOR THIS EXERCISE YOU SHOULD MODIFY THEM! ===== - To run the tests for this exercise, run `npm test -- --testPathPattern 6-writing-tests-advanced.js` - To run all exercises/tests in the mandatory folder, run `npm test` - (Reminder: You must have run `npm install` one time before this will work!) */ -/* - Write a test that checks the output of formatCourseworkResult when passed the following trainee: - { +/*Write a test that checks the output of formatCourseworkResult when passed the following trainee */ +test("formatCourseworkResult works for trainee Xin", () => { + const trainee1 = { name: "Xin", score: 63 - } -*/ + }; + + const result = formatCourseworkResult(trainee1); + + expect(result).toEqual("Xin's coursework was marked as grade C."); +}); + + /* - Write a test that checks the output of formatCourseworkResult when passed the following trainee: - { + Write a test that checks the output of formatCourseworkResult when passed the following trainee:*/ + let trainee2 = { name: "Mona", score: 78 } -*/ + test("formatCourseworkResult works for trainee Mona", () => { + const trainee2= { + name: "Mona", + score: 78 + }; + + const result = formatCourseworkResult(trainee2); + + expect(result).toEqual("Mona's coursework was marked as grade B."); + }); /* - Write a test that checks the output of formatCourseworkResult when passed the following trainee: - { - name: "Ali", - score: 49, - age: 33, - subjects: ["JavaScript", "React", "CSS"] - } -*/ + Write a test that checks the output of formatCourseworkResult when passed the following trainee:*/ + test("formatCourseworkResult takes name and grade", () => { + let trainee3 = { + name: "Ali", + score: 49, + age: 33, + subjects: ["JavaScript", "React", "CSS"] + } + + const result = formatCourseworkResult(trainee3); + + expect(result).toEqual("Ali's coursework was marked as grade E."); + }); + /* - Write a test that checks the output of formatCourseworkResult when passed the following trainee: - { - score: 90, - age: 29 - } -*/ + Write a test that checks the output of formatCourseworkResult when passed the following trainee:*/ + test("formatCourseworkResult works for no name ", () => { + let trainee4 = { + score: 90, + age: 29 + } + + const result = formatCourseworkResult(trainee4); + + expect(result).toEqual("Error: No trainee name!"); + }); + /* - Write a test that checks the output of formatCourseworkResult when passed the following trainee: - { - name: "Aman", - subjects: ["HTML", "CSS", "Databases"] - } -*/ + Write a test that checks the output of formatCourseworkResult when passed the following trainee:*/ + test("formatCourseworkResult works for no score ", () => { + let trainee5 = { + name: "Aman", + subjects: ["HTML", "CSS", "Databases"] + } + + const result = formatCourseworkResult(trainee5); + + expect(result).toEqual("Error: Coursework percent is not a number!"); + }); +