Skip to content
This repository was archived by the owner on Jan 14, 2024. It is now read-only.
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 9 additions & 6 deletions 1-exercises/A-accessing-values/exercise1.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,19 +5,22 @@
*/

let dog = {
breed: "Dalmatian",
name: "Spot",
isHungry: true,
happiness: 6
breed: "Dalmatian", // string
name: "Spot", // string
isHungry: true, // boolean
happiness: 6 // number
};


/*
You can access the values of each property using dot notation.
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}`);

Expand Down
2 changes: 1 addition & 1 deletion 1-exercises/A-accessing-values/exercise2.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ let capitalCities = {
*/

let myCountry = "UnitedKingdom";
let myCapitalCity; // complete the code
let myCapitalCity = capitalCities[myCountry]; // complete the code

console.log(myCapitalCity);

Expand Down
17 changes: 17 additions & 0 deletions 1-exercises/A-accessing-values/exercise3.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,16 +13,33 @@ let basketballTeam = {
},
};

// console.log(basketballTeam.topPlayers.sort());

// for (let player of basketballTeam.topPlayers) {
// console.log(player);
// }

/*
Write code that
- accesses the basketball team's top players array
- sorts the top players in alphabetical order
- console.logs the name of each player on a new line
*/

function displayPlayerNamesAlphabetically() {
let topPlayersArray = basketballTeam.topPlayers;
topPlayersArray.sort();
for (let playerName of topPlayersArray) {
console.log(playerName);
}
}

displayPlayerNamesAlphabetically();

// write code here



/* EXPECTED RESULT

Dennis Rodman
Expand Down
7 changes: 6 additions & 1 deletion 1-exercises/B-setting-values/exercise1.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,12 @@ let capitalCities = {
*/

// write code here

capitalCities.UnitedKingdom.population = 8980000;
capitalCities.China.population = 21500000;
capitalCities.China.population = 21500000;
capitalCities.Peru = {};
capitalCities.Peru.name = "Lima";
capitalCities.Peru.population = 9750000;
console.log(capitalCities);

/* EXPECTED RESULT
Expand Down
6 changes: 6 additions & 0 deletions 1-exercises/B-setting-values/exercise2.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@ let student = {
*/

// write code here
student.attendance = {};
student.attendance = 90;

/*
- Write an "if" statement that changes the value of hasPassed to true
Expand All @@ -25,6 +27,10 @@ let student = {
- Use bracket notation to change the value of hasPassed
*/

if (student.attendance >= 90 && student.examScore > 60) {
student["hasPassed"] = true;
}

// write code here

console.log(student);
Expand Down
6 changes: 6 additions & 0 deletions 1-exercises/C-undefined-properties/exercise.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@ let car = {

console.log(car["colour"]);

// We see undefined because the property "colour" is absent from the object.

// Example 2
function sayHelloToUser(user) {
console.log(`Hello ${user.firstName}`);
Expand All @@ -27,6 +29,8 @@ let user = {

sayHelloToUser(user);

// There is not "firstName" property in the object. So we get undefined.

// Example 3
let myPet = {
animal: "Cat",
Expand All @@ -36,3 +40,5 @@ let myPet = {
};

console.log(myPet.getName());

// There is no return inside the function which makes it undefined.
3 changes: 3 additions & 0 deletions 1-exercises/D-object-methods/exercise.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,9 @@
*/

let student = {
getName: function(name) {
console.log(`Student name: ${name}`);
}
// write code here
}

Expand Down
73 changes: 73 additions & 0 deletions 2-mandatory/1-recipes.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,4 +22,77 @@
You should write and log at least 5 recipes
*/

let favouriteRecipe1 = {};

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This code looks correct to me 👍
One small comment - usually if you're defining an object and you already know the properties and values, it can be easier (to both write and read) to include all the information in your object definition. Using your example, this will look like:

let favouriteRecipe1 = {
    title: "Mole",
    servings: 2,
    ingredients: ["cinnamon", "cumin", "cocoa"]
};

favouriteRecipe1.title = "Mole";
favouriteRecipe1.servings = 2;
favouriteRecipe1.ingredients = ["cinnamon", "cumin", "cocoa"];


Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Seems like we have a lot of duplicate code here to output a recipe in the format that we want.
Can you use a function to reduce the duplication?

console.log(favouriteRecipe1.title);
console.log(`serves: ${favouriteRecipe1.servings}`);
console.log("Ingredients: ");
for (let ingredient of favouriteRecipe1.ingredients) {
console.log(ingredient);
}

console.log();

let favouriteRecipe2 = {};

favouriteRecipe2.title = "Ramen";
favouriteRecipe2.servings = 1;
favouriteRecipe2.ingredients = ["ramen cake", "egg", "spices", "chicken broth", "veggies"];

console.log(favouriteRecipe2.title);
console.log(`serves: ${favouriteRecipe2.servings}`);
console.log("Ingredients: ");
for (let ingredient of favouriteRecipe2.ingredients) {
console.log(ingredient);
}

console.log();

let favouriteRecipe3 = {};

favouriteRecipe3.title = "Dried Mango";
favouriteRecipe3.servings = 3;
favouriteRecipe3.ingredients = ["mango", "seasoning", "oil"];

console.log(favouriteRecipe3.title);
console.log(`serves: ${favouriteRecipe3.servings}`);
console.log("Ingredients: ");
for (let ingredient of favouriteRecipe3.ingredients) {
console.log(ingredient);
}

console.log();

let favouriteRecipe4 = {};

favouriteRecipe4.title = "Cacao Cake";
favouriteRecipe4.servings = 6;
favouriteRecipe4.ingredients = ["cacao", "maple syrup", "eggs", "coconut oil"];

console.log(favouriteRecipe4.title);
console.log(`serves: ${favouriteRecipe4.servings}`);
console.log("ingredients: ");
for (let ingredient of favouriteRecipe4.ingredients) {
console.log(ingredient);
}

console.log();

let favouriteRecipe5 = {};

favouriteRecipe5.title = "Pad Thai";
favouriteRecipe5.servings = 3;
favouriteRecipe5.ingredients = ["glass noodles", "sauce", "shrimp", "peanuts", "eggs", "garlic, veggies"];

console.log(favouriteRecipe5.title);
console.log(`serves: ${favouriteRecipe5.servings}`);
console.log("Ingredients: ");
for (let ingredient of favouriteRecipe5.ingredients) {
console.log(ingredient);
}
// write code here
3 changes: 3 additions & 0 deletions 2-mandatory/2-currency-code-lookup.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,12 @@ const COUNTRY_CURRENCY_CODES = [
];

function createLookup(countryCurrencyCodes) {
return Object.fromEntries(countryCurrencyCodes);

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Clever 😄

// write code here
}

createLookup(COUNTRY_CURRENCY_CODES);

/* ======= TESTS - DO NOT MODIFY =====
- To run the tests for this exercise, run `npm test -- --testPathPattern 2-currency-code-lookup.js`
- To run all exercises/tests in the mandatory folder, run `npm test`
Expand Down
11 changes: 11 additions & 0 deletions 2-mandatory/3-shopping-list.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,17 @@ let pantry = {
};

function createShoppingList(recipe) {

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This implementation looks good to me!
For an extra challenge - can you re-write this using the filter array method?

let newItems = [];
for (let ingredient of recipe.ingredients) {
if (!pantry.fridgeContents.includes(ingredient) && !pantry.cupboardContents.includes(ingredient)) {
newItems.push(ingredient);
}
}
let result = {
"name": recipe.name,
items: newItems,
}
return result;
// write code here
}

Expand Down
20 changes: 18 additions & 2 deletions 2-mandatory/4-restaurant.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,23 @@ const MENU = {
};

let cashRegister = {
// write code here
}
orderBurger: function(balance){
if (balance >= MENU.burger) {
balance = balance - MENU.burger;
} else {

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do you think the else clause is needed here? What happens if you remove it?

balance = balance;
}
return (balance);
},
orderFalafel: function(balance){
if(balance >= MENU.falafel) {
balance = balance - MENU.falafel;
} else {
balance = balance;
}
return (balance);
}
};

/* ======= TESTS - DO NOT MODIFY =====
- To run the tests for this exercise, run `npm test -- --testPathPattern 4-restaurant.js`
Expand All @@ -48,3 +63,4 @@ test("orderFalafel will not subtract from balance if balance is too low", () =>
let balance = 7.24;
expect(cashRegister.orderFalafel(balance)).toEqual(7.24);
});

30 changes: 19 additions & 11 deletions 2-mandatory/5-writing-tests.js
Original file line number Diff line number Diff line change
Expand Up @@ -35,41 +35,49 @@ function convertScoreToGrade(score) {
passes.
*/
test("a score of 83 is grade A", () => {
expect(convertScoreToGrade(83), "Z");
expect(convertScoreToGrade(83)).toEqual("A");
});

/*
The rest of the tests have comments describing what to test and you need to
write a matching test
*/

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice job 👍

test.skip("a score of 71 is grade B", () => {
/* Remove the .skip above, then write the test body. */
test("a score of 71 is grade B", () => {
expect(convertScoreToGrade(71)).toEqual("B");
});
/*
Write a test that checks a score of 68 is grade C
*/
test("a score of 68 is grade C", () => {
expect(convertScoreToGrade(68)).toEqual("C");
});

/*
Write a test that checks a score of 55 is grade D
*/

/*
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 D", () => {
expect(convertScoreToGrade(55)).toEqual("D");
});

/*
Write a test that checks a score of 49 is grade E
*/
test("a score of 49 is grade E", () => {
expect(convertScoreToGrade(49)).toEqual("E");
});

/*
Write a test that checks a score of 30 is grade E
*/

test("a score of 30 is grade E", () => {
expect(convertScoreToGrade(30)).toEqual("E");
});

/*
Write a test that checks a score of 70 is grade B
*/
test("a score of 70 is grade B", () => {
expect(convertScoreToGrade(70)).toEqual("B");
});
Loading