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
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
12 changes: 12 additions & 0 deletions 1-exercises/A-accessing-values/exercise3.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,18 @@ let basketballTeam = {

// write code here

// Access the top players array
let topPlayers = basketballTeam.topPlayers;

// Sort the top players in alphabetical order
topPlayers.sort();

// Log the name of each player on a new line
console.log("Top Players:");
topPlayers.forEach(player => {
console.log(player);
});


/* EXPECTED RESULT

Expand Down
11 changes: 11 additions & 0 deletions 1-exercises/B-setting-values/exercise1.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,17 @@ let capitalCities = {

// write code here


capitalCities.UnitedKingdom.population = 8980000;

capitalCities.China.population = 21500000;

capitalCities.Peru = {};

capitalCities.Peru.name = "Lima";

capitalCities.Peru.population = 9750000;

console.log(capitalCities);

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

// write code here

student['attendance'] = 90;

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

console.log("Has Passed:", student['hasPassed']);

console.log(student);

/* EXPECTED RESULT
Expand Down
3 changes: 3 additions & 0 deletions 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"]);
// the "car" object does not have a property called "colour", so it returns undefined.

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

sayHelloToUser(user);
//the "user" object does not have a property called "firstName", as it only has a "name" property.

// Example 3
let myPet = {
Expand All @@ -36,3 +38,4 @@ let myPet = {
};

console.log(myPet.getName());
//the function inside "getName" does not have a return statement.
4 changes: 3 additions & 1 deletion 1-exercises/D-object-methods/exercise.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,9 @@
*/

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

student.getName("Daniel");
Expand Down
72 changes: 71 additions & 1 deletion 2-mandatory/1-recipes.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,4 +22,74 @@
You should write and log at least 5 recipes
*/

// write code here
// write code here

// Recipe 1
let recipe1 = {
title: "Mole",
servings: 2,
ingredients: ["cinnamon", "cumin", "cocoa"]
};

console.log(recipe1.title);
console.log("Serves: " + recipe1.servings);
console.log("Ingredients:");
for (let i = 0; i < recipe1.ingredients.length; i++) {
console.log(recipe1.ingredients[i]);
}

// Recipe 2
let recipe2 = {
title: "Pasta Carbonara",
servings: 4,
ingredients: ["spaghetti", "eggs", "bacon", "parmesan cheese", "black pepper"]
};

console.log(recipe2.title);
console.log("Serves: " + recipe2.servings);
console.log("Ingredients:");
for (let i = 0; i < recipe2.ingredients.length; i++) {
console.log(recipe2.ingredients[i]);
}

// Recipe 3
let recipe3 = {
title: "Chicken Alfredo",
servings: 6,
ingredients: ["chicken breast", "butter", "garlic", "heavy cream", "parmesan cheese", "parsley"]
};

console.log(recipe3.title);
console.log("Serves: " + recipe3.servings);
console.log("Ingredients:");
for (let i = 0; i < recipe3.ingredients.length; i++) {
console.log(recipe3.ingredients[i]);
}

// Recipe 4
let recipe4 = {
title: "Beef Stroganoff",
servings: 4,
ingredients: ["beef sirloin", "onion", "mushrooms", "sour cream", "beef broth", "flour"]
};

console.log(recipe4.title);
console.log("Serves: " + recipe4.servings);
console.log("Ingredients:");
for (let i = 0; i < recipe4.ingredients.length; i++) {
console.log(recipe4.ingredients[i]);
}

// Recipe 5
let recipe5 = {
title: "Vegetable Stir-Fry",
servings: 3,
ingredients: ["bell peppers", "broccoli", "carrots", "onion", "garlic", "soy sauce"]
};

console.log(recipe5.title);
console.log("Serves: " + recipe5.servings);
console.log("Ingredients:");
for (let i = 0; i < recipe5.ingredients.length; i++) {
console.log(recipe5.ingredients[i]);
}
14 changes: 13 additions & 1 deletion 2-mandatory/2-currency-code-lookup.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,21 @@ const COUNTRY_CURRENCY_CODES = [
];

function createLookup(countryCurrencyCodes) {
// write code here
let lookup = {};

for (let i = 0; i < countryCurrencyCodes.length; i++) {
let countryCode = countryCurrencyCodes[i][0];
let currencyCode = countryCurrencyCodes[i][1];

lookup[countryCode] = currencyCode;
}

return lookup;
}

const countryCurrencyLookup = createLookup(COUNTRY_CURRENCY_CODES);
console.log(countryCurrencyLookup);

/* ======= 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
21 changes: 20 additions & 1 deletion 2-mandatory/3-shopping-list.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,27 @@ let pantry = {
};

function createShoppingList(recipe) {
// write code here
let missingIngredients = [];
for (let i = 0; i < recipe.ingredients.length; i++) {
let ingredient = recipe.ingredients[i];
if (!pantry.fridgeContents.includes(ingredient) && !pantry.cupboardContents.includes(ingredient)) {
missingIngredients.push(ingredient);
}
}
let shoppingList = {
name: recipe.name,
items: missingIngredients
};
return shoppingList;
}
let recipe = {
name: "Spaghetti Bolognese",
ingredients: ["spaghetti", "minced beef", "onion", "garlic", "tomato sauce", "herbs", "cheese"]
};

let shoppingList = createShoppingList(recipe);
console.log(shoppingList);


/* ======= TESTS - DO NOT MODIFY =====
- To run the tests for this exercise, run `npm test -- --testPathPattern 3-shopping-list.js`
Expand Down
21 changes: 18 additions & 3 deletions 2-mandatory/4-restaurant.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,15 +13,30 @@

Add another method to the cashRegister object which is called orderFalafel and handles ordering a falafel, in the same way as ordering a burger.
*/

const MENU = {
burger: 6.5,
falafel: 7.25,
};

let cashRegister = {
// write code here
}
// method to order a burger
orderBurger: function(balance) {
if (balance >= MENU.burger) {
balance -= MENU.burger;
}
return balance;
},

// method to order a falafel
orderFalafel: function(balance) {
if (balance >= MENU.falafel) {
balance -= MENU.falafel;
}
return balance;
}
};



/* ======= TESTS - DO NOT MODIFY =====
- To run the tests for this exercise, run `npm test -- --testPathPattern 4-restaurant.js`
Expand Down
30 changes: 29 additions & 1 deletion 3-extra/1-count-words.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,13 +23,41 @@
- Setting values on an object
*/

// function countWords(string) {
// const wordCount = {};
// const words = string.toLowerCase().split(" ");
// words.forEach(word => {
// if (!wordCount.hasOwnProperty(word)) {
// wordCount[word] = 1;
// } else {
// wordCount[word] ++;
// }
// });

// return wordCount;
// }

// below is the solution :

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

// write code here
let arrayOfWords = string.split(" ");

arrayOfWords.forEach((word) => {
if (word !== "") {
if (wordCount.hasOwnProperty(word)) {
wordCount[word] += 1;
} else {
wordCount[word] = 1;
}
}
});

return wordCount;
}



/* ======= TESTS - DO NOT MODIFY =====
- To run the tests for this exercise, run `npm run extra-tests`
Expand Down