Skip to content
This repository was archived by the owner on Jan 14, 2024. It is now read-only.
Closed
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
4 changes: 2 additions & 2 deletions 1-exercises/A-accessing-values/exercise1.js
Original file line number Diff line number Diff line change
Expand Up @@ -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}`);

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
2 changes: 1 addition & 1 deletion 1-exercises/A-accessing-values/exercise3.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ let basketballTeam = {
*/

// write code here

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

/* EXPECTED RESULT

Expand Down
4 changes: 3 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,9 @@ let capitalCities = {
*/

// write code here

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

/* EXPECTED RESULT
Expand Down
9 changes: 5 additions & 4 deletions 1-exercises/B-setting-values/exercise2.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,15 @@
let student = {
name: "Reshma Saujani",
examScore: 65,
hasPassed: false
hasPassed: false,
};

/*
Using bracket notation
- Add a property to the student object for attendance
- Set the value of attendance to 90
*/

student["attendance"] = 90;
// write code here

/*
Expand All @@ -26,7 +26,8 @@ let student = {
*/

// write code here

if (student.attendance >= 90 && student.examScore > 60)
student.hasPassed = true;
console.log(student);

/* EXPECTED RESULT
Expand All @@ -38,4 +39,4 @@ console.log(student);
attendance: 90
}

*/
*/
4 changes: 3 additions & 1 deletion 1-exercises/C-undefined-properties/exercise.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ let car = {
};

console.log(car["colour"]);
//Because colour is not defined in car

// Example 2
function sayHelloToUser(user) {
Expand All @@ -26,7 +27,7 @@ let user = {
};

sayHelloToUser(user);

//Because firstName is not defined
// Example 3
let myPet = {
animal: "Cat",
Expand All @@ -36,3 +37,4 @@ let myPet = {
};

console.log(myPet.getName());
//Because the function is not returning nothing
1 change: 1 addition & 0 deletions 1-exercises/D-object-methods/exercise.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
*/

let student = {
getName:(name)=>console.log("Student name: " +name)
// write code here
}

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

// write code here
// write code here
let recipes = {
carbonara: {
title: "Carbonara",
serves: 2,
ingredients: ["Spaghetti", "Guanciale", "Egg", "Cheese"],
},
pizza: {
title: "Pizza",
serves: 1,
ingredients: [
"Flour",
"Salt",
"Olive oil",
"Mozzarella",
"Passata",
"Parmesan",
],
},
tiramisu: {
title: "Tiramisu",
serves: 3,
ingredients: ["Mascarpone", "Egg", "Savoiardi", "Sugar", "Caffè"],
},
amatriciana: {
title: "Amatriciana",
serves: 2,
ingredients: [
"Spaghetti",
"passata",
"Guanciale",
"Cheese",
"Salt",
"Oil",
"White wine",
],
},
steakBroccoli: {
title: "Steak and Broccoli",
serves: 2,
ingredients: [
"Wholegrain",
"Chopped sushi ginger",
"Spring onions",
"Broccoli",
"Fillet steak",
],
},
};
for (let recipe in recipes){
console.log(recipes[recipe].title);

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

You use recipes[recipe] multiple times. I usually suggest introducing a variable to reduce code duplication. But in this case - would for-of loop work better?

console.log("Serves: " + recipes[recipe].serves);
console.log("Ingredients:");
recipes[recipe].ingredients.forEach(element => {console.log(element)
});
console.log("\n");
}
2 changes: 2 additions & 0 deletions 2-mandatory/2-currency-code-lookup.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,10 @@ const COUNTRY_CURRENCY_CODES = [
];

function createLookup(countryCurrencyCodes) {
return Object.fromEntries(countryCurrencyCodes);
// write code here
}
console.log(createLookup(COUNTRY_CURRENCY_CODES));

/* ======= TESTS - DO NOT MODIFY =====
- To run the tests for this exercise, run `npm test -- --testPathPattern 2-currency-code-lookup.js`
Expand Down
24 changes: 18 additions & 6 deletions 2-mandatory/3-shopping-list.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,16 +10,21 @@

The createShoppingList function should return an object with two properties:
- "name" of the recipe, which is a string,
- "items", which is an arry of the missing ingredients that need to be on the shopping list
- "items", which is an array of the missing ingredients that need to be on the shopping list
*/

let pantry = {
fridgeContents: ["butter", "milk"],
cupboardContents: ["salt", "tinned tomatoes", "oregano"],
};

function createShoppingList(recipe) {
// write code here
function createShoppingList({ name, ingredients }) {
const { cupboardContents,fridgeContents} = pantry;

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

👍

const myItems = [...cupboardContents, ...fridgeContents].reduce(
(acc, item) => ({ ...acc, [item]: 1 }),
{}
);
return { name, items: ingredients.filter((item) => !myItems[item]) };
}

/* ======= TESTS - DO NOT MODIFY =====
Expand All @@ -43,11 +48,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"],
});
});
});
15 changes: 13 additions & 2 deletions 2-mandatory/4-restaurant.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,20 @@ const MENU = {
falafel: 7.25,
};

const order = (price) => (balance) =>

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Very good - functions that produce functions is an important mental shift. That's a very powerful technique. Don't overuse it though :)

balance >= price ? balance - price : balance;

let cashRegister = {
// write code here
}
orderBurger: order(MENU.burger),
orderFalafel: order(MENU.falafel),
};

// let cashRegister = {
// orderBurger: (balance) =>
// balance >= MENU.burger ? balance - MENU.burger : balance,
// orderFalafel: (balance) =>
// balance >= MENU.falafel ? balance - MENU.falafel : balance,
// };

/* ======= TESTS - DO NOT MODIFY =====
- To run the tests for this exercise, run `npm test -- --testPathPattern 4-restaurant.js`
Expand Down
26 changes: 17 additions & 9 deletions 3-extra/1-count-words.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,11 +24,11 @@
*/

function countWords(string) {
const wordCount = {};

// write code here

return wordCount;
return (string ? string.split(" ") : []).reduce(

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 is a decent implementation and you used quite a few tricks here. However, I ague that you need to look for simpler (more readable) solutions:

function countWords(string) {
  const wordCount = {};
  const words = string ? string.split(" ") : []
  for (const word of words) {
    wordCount[word] = 1 + (wordCount[word] || 0)
  }
  return wordCount
}

(acc, item) => ({ ...acc, [item]: acc[item] ? ++acc[item] : 1 }),
{}
);
// string.split()
}

/* ======= TESTS - DO NOT MODIFY =====
Expand All @@ -46,17 +46,25 @@ 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", () => {
expect(countWords("")).toEqual({});
});

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,
Expand Down