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
8 changes: 4 additions & 4 deletions 1-exercises/A-accessing-values/exercise1.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,21 +8,21 @@ let dog = {
breed: "Dalmatian",
name: "Spot",
isHungry: true,
happiness: 6
happiness: 6,
};

/*
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}`);

/* EXPECTED RESULT

Spot is a Dalmatian

*/
*/
6 changes: 3 additions & 3 deletions 1-exercises/A-accessing-values/exercise2.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
let capitalCities = {
UnitedKingdom: "London",
China: "Beijing",
Peru: "Lima"
Peru: "Lima",
};

/*
Expand All @@ -17,12 +17,12 @@ let capitalCities = {
*/

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

console.log(myCapitalCity);

/* EXPECTED RESULT

London

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

// write code here

basketballTeam["topPlayers"].sort().forEach((x) => console.log(x));

/* EXPECTED RESULT

Dennis Rodman
Michael Jordan
Scottie Pippen

*/
*/
7 changes: 5 additions & 2 deletions 1-exercises/B-setting-values/exercise1.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ let capitalCities = {
},
China: {
name: "Beijing",
}
},
};

/*
Expand All @@ -24,6 +24,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 All @@ -34,4 +37,4 @@ console.log(capitalCities);
Peru: { name: "Lima", population: 9750000 }
}

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

/*
Expand All @@ -16,6 +16,10 @@ let student = {
*/

// write code here
student["attendance"] = 90;
student["attendance"] >= 90 && student["examScore"] > 60
? (student["hasPassed"] = true)
: (student["hasPassed"] = false);

/*
- Write an "if" statement that changes the value of hasPassed to true
Expand All @@ -38,4 +42,4 @@ console.log(student);
attendance: 90
}

*/
*/
7 changes: 5 additions & 2 deletions 1-exercises/C-undefined-properties/exercise.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,24 +15,27 @@ let car = {
};

console.log(car["colour"]);
//there is no key name of "colour" in the variable name car.

// Example 2
function sayHelloToUser(user) {
console.log(`Hello ${user.firstName}`);
}

let user = {
name: "Mira"
name: "Mira",
};

sayHelloToUser(user);
///there is no key name of "firstName" in the variable name user.

// Example 3
let myPet = {
animal: "Cat",
getName: function() {
getName: function () {
"My pet's name is Fluffy";
},
};

console.log(myPet.getName());
//it has to insert "return" on the "My pet's name is Fluffy"
7 changes: 5 additions & 2 deletions 1-exercises/D-object-methods/exercise.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,15 @@

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

student.getName("Daniel");

/* EXPECTED RESULT

Student name: Daniel

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

// write code here
// write code here
const recipe1 = {
title: "Mole1",
Serves: 2,
Ingredients: ["cinnamon1", "cumin1", "cocoa1"],
};
const recipe2 = {
title: "Mole2",
Serves: 2,
Ingredients: ["cinnamon2", "cumin2", "cocoa2"],
};
const recipe3 = {
title: "Mole3",
Serves: 2,
Ingredients: ["cinnamon3", "cumin3", "cocoa3"],
};
const recipe4 = {
title: "Mole4",
Serves: 2,
Ingredients: ["cinnamon4", "cumin4", "cocoa4"],
};
const recipe5 = {
title: "Mole5",
Serves: 2,
Ingredients: ["cinnamon5", "cumin5", "cocoa5"],
};
const recipeArr = [recipe1, recipe2, recipe3, recipe4, recipe5];
for (let i = 0; i < recipeArr.length; i++) {
for (const property in recipeArr[i]) {
if (property === "title") {
console.log(`${recipeArr[i][property]}`);
} else if (property === "Serves") {
console.log(`${property}:${recipeArr[i][property]}`);
} else if (property === "Ingredients") {
console.log(`${property}:`);
Array.from(recipeArr[i][property]).forEach((x) => console.log(x));
console.log(`----------------------`);
}
}
}
// Result:
// Mole1
// Serves:2
// Ingredients:
// cinnamon1
// cumin1
// cocoa1
// ----------------------
// Mole2
// Serves:2
// Ingredients:
// cinnamon2
// cumin2
// cocoa2
// ----------------------
// Mole3
// Serves:2
// Ingredients:
// cinnamon3
// cumin3
// cocoa3
// ----------------------
// Mole4
// Serves:2
// Ingredients:
// cinnamon4
// cumin4
// cocoa4
// ----------------------
// Mole5
// Serves:2
// Ingredients:
// cinnamon5
// cumin5
// cocoa5
// ----------------------
13 changes: 11 additions & 2 deletions 2-mandatory/2-currency-code-lookup.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,19 @@ const COUNTRY_CURRENCY_CODES = [
["MX", "MXN"],
];

// Object.fromEntries way:
function createLookup(countryCurrencyCodes) {
// write code here
return Object.fromEntries(countryCurrencyCodes);
}

//---another way if like "reduce" method---:
// function createLookup(countryCurrencyCodes) {
// return countryCurrencyCodes.reduce((acc, cur) => {
// acc[cur[0]] = cur[1];
// return acc;
// }, {});
// }

/* ======= 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 All @@ -34,4 +43,4 @@ test("creates country currency code lookup", () => {
NG: "NGN",
MX: "MXN",
});
});
});
24 changes: 20 additions & 4 deletions 2-mandatory/3-shopping-list.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,16 @@ let pantry = {
};

function createShoppingList(recipe) {
// write code here
const resultObj = {};
resultObj.name = recipe.name;
resultObj.items = [];
const pantryAllList = Object.values(pantry).flat();

for (let i = 0; i < recipe.ingredients.length; i++) {
if (!pantryAllList.includes(recipe.ingredients[i]))
resultObj.items.push(recipe.ingredients[i]);
}
Comment on lines +25 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.

There is a more efficient method of doing this:

  const pantryAllSet = new Set(Object.values(pantry).flat())
  resultObj.items = [...new Set(recipe.ingredients.filter(x => !pantryAllSet.has(x)))]

return resultObj;
}

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

let cashRegister = {
// write code here
}
orderBurger: (balance) => {
return balance >= MENU.burger ? balance - MENU.burger : balance;
},
orderFalafel: (balance) => {
return 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
32 changes: 25 additions & 7 deletions 3-extra/1-count-words.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,19 @@
*/

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

// write code here
let wordCount = {};
let arr = [];
if (string.length > 0) {
arr = string.split(" ");
}
Comment on lines +29 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.

What is the output if we input countWords(" ")?

Copy link
Copy Markdown
Author

@susanssky susanssky Dec 17, 2022

Choose a reason for hiding this comment

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

If would like { '': 1 } instead of { '': 2 }, we can insert trim(). arr = string.trim().split(" ");

wordCount = arr.reduce((acc, cur) => {
if (cur in acc) {
acc[cur]++;
} else {
acc[cur] = 1;
}
return acc;
}, {});

return wordCount;
}
Expand All @@ -46,17 +56,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