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
12 changes: 6 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,19 @@
*/

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["UnitedKingdom"]; // complete the code
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 works, but you have the myCountry variable available to you so you can also use this in the bracket notation :)


console.log(myCapitalCity);

Expand Down
2 changes: 2 additions & 0 deletions 1-exercises/A-accessing-values/exercise3.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,9 @@ let basketballTeam = {
*/

// write code here
let topPlayersArr = basketballTeam.topPlayers.sort();

topPlayersArr.forEach(player => console. log(player));
Comment on lines +24 to +26
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

really good, well done


/* EXPECTED RESULT

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

// write code here
capitalCities.UnitedKingdom.population = 8980000;
capitalCities.China.population = 21500000;
capitalCities.Peru = {};
capitalCities.Peru.name = "Lima";
capitalCities.Peru.population = 975000;
Comment on lines +28 to +30
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 works, you can also set the properties of the Peru object all at once, by setting the name & population when you create the object


console.log(capitalCities);

Expand Down
5 changes: 5 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,11 @@ let student = {
*/

// write code here
student["attendence"] = 90;
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

good use of bracket notation here


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

/*
- Write an "if" statement that changes the value of hasPassed to true
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"]);

// Because there's no such a property named colour in the object car.

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

sayHelloToUser(user);

// Because there's no such a property called firstName in the object user.

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

console.log(myPet.getName());

// Because in the object method, it omits the keyword return for the string.
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

good explanations for why JavaScript is returning 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 @@ -9,6 +9,9 @@

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

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

// write code here
// write code here

let chicken = {
title:"chicken",
serves:4,
ingredients: ["chicken","potatoes"]
}

let pork = {
title:"pork",
serves:4,
ingredients: ["pork","tomtoes"]
}

let steak = {
title:"steak",
serves:2,
ingredients: ["beef","pepper"]
}

let lamb = {
title:"lamb",
serves:3,
ingredients: ["lamb","garlic"]
}

let fish = {
title:"fish",
serves:6,
ingredients: ["fish","onion"]
}
console.log(Object.entries(fish));

function printRecipe(obj) {
Copy link
Copy Markdown

@Nomes27 Nomes27 Oct 16, 2022

Choose a reason for hiding this comment

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

this is really good and it's clear you have a good understanding of accessing object keys & values. The question asks for each recipe to be in this format when console.log is used:

  Mole
  Serves: 2
  Ingredients:
  cinnamon
  cumin
  cocoa

Can you think of ways you might change your approach so that it follows this format? (for example not having the title key logged & having the ingredients on a new line)

for ([key, value] of Object.entries(obj)) {
console.log(`${key}: ${value}`);
}
}

printRecipe(chicken);
printRecipe(pork);
printRecipe(steak);
printRecipe(lamb);
printRecipe(fish);


5 changes: 5 additions & 0 deletions 2-mandatory/2-currency-code-lookup.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,11 @@ const COUNTRY_CURRENCY_CODES = [

function createLookup(countryCurrencyCodes) {
// write code here
let result = {};
for (let [country, currency] of countryCurrencyCodes) {
result[country] = currency;
}
return result;
Comment on lines +22 to +26
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

really good, well done :)

}

/* ======= TESTS - DO NOT MODIFY =====
Expand Down
21 changes: 18 additions & 3 deletions 2-mandatory/3-shopping-list.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,14 @@ let pantry = {

function createShoppingList(recipe) {
// write code here
let result = {};
result.name = recipe["name"];
let pantryArr = pantry.fridgeContents.concat(pantry.cupboardContents);
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

I really like how you use concat here to combine the fridge & cupboard items first to make it easier to search through

let missingArr = recipe["ingredients"].filter(
(food) => pantryArr.indexOf(food) === -1
);
result.items = missingArr;
return result;
}

/* ======= TESTS - DO NOT MODIFY =====
Expand All @@ -43,11 +51,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"],
});
});
});
19 changes: 18 additions & 1 deletion 2-mandatory/4-restaurant.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,24 @@ const MENU = {

let cashRegister = {
// write code here
}
orderBurger: function (balance) {
if (balance >= MENU.burger) {
let newBalance = balance - MENU.burger;
return newBalance;
} else {
return balance;
}
},
Comment on lines +24 to +31
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

good job here, this is a very clear solution & your variable names make it easy to understand if you are unfamiliar with the code

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


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

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

if (!string || string.trim().length === 0) {
return wordCount;
}
let strArr = string.split(" ");
strArr.map((word) => {
if (wordCount[word] === undefined) {
wordCount[word] = 1;
} else {
wordCount[word] = wordCount[word] + 1;
}
});
// write code here

return wordCount;

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

great job making it onto the extra tasks and on having passing tests! A shorthand way of doing wordCount[word] = wordCount[word] + 1; would be wordCount[word] += 1;

}

/* ======= TESTS - DO NOT MODIFY =====
Expand All @@ -46,17 +57,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