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

// write code here
const topPlayers = (basketballTeam.topPlayers).sort()
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 here since all the strings use upper case for the first letter. Be careful with using sort for strings without passing a custom function. This is because all uppercase letters are "smaller" than lower case ones. So "M" for example would be sorted before "a". Here is an article explaining in more detail and how to handle it if you want to make your sort case insensitive https://www.danywalls.com/understand-how-the-sort-method-operates-on-javascript-arrays


for (players of topPlayers){
console.log(players);
}



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

// write code here
capitalCities.UnitedKingdom.population = 8980000;
capitalCities.China.population = 21500000;
capitalCities.Peru = {name:'Lima',population:9750000};

console.log(capitalCities);

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

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

/*
- Write an "if" statement that changes the value of hasPassed to true
Expand All @@ -26,6 +27,9 @@ let student = {
*/

// write code here
if(student.attendance >= 90 && student.examScore > 60){
student['hasPassed'] = true;
}

console.log(student);

Expand Down
6 changes: 3 additions & 3 deletions 1-exercises/C-undefined-properties/exercise.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,10 @@ let car = {
brand: "Ford",
yearsOld: 8,
};

// no colour property defined
console.log(car["colour"]);

// Example 2
// Example 2 the property firstName is not defined
function sayHelloToUser(user) {
console.log(`Hello ${user.firstName}`);
}
Expand All @@ -34,5 +34,5 @@ let myPet = {
"My pet's name is Fluffy";
},
};

//we should log myPet.getName only () not required
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 not the case. getName is indeed a function, so we need to use () to call it. The problem here is that the get name function is not returning anything, so it returns undefined. It should be:

getName: function() {
  return "My pet's name is Fluffy";
}

console.log(myPet.getName());
6 changes: 5 additions & 1 deletion 1-exercises/D-object-methods/exercise.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,13 @@

let student = {
// write code here
getName: function getName(name){
return name
}
Comment on lines +12 to +14
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 👏

}

student.getName("Daniel");
let studentName = student.getName("Daniel");
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

It doesn't affect the code working, but we should only use let to define a variable if we are going to change it later on. In other cases, we should use const. This makes our code "safer" as we will only be able to reassign variables that we define as let, and expect to be changed. (Note: there are other subtle differences with let/const related to scope, but I wouldn't advise worrying about it at this stage).

console.log(`Student name: ${studentName}`)

/* EXPECTED RESULT

Expand Down
26 changes: 26 additions & 0 deletions 2-mandatory/1-recipes.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,5 +21,31 @@

You should write and log at least 5 recipes
*/
let favouriteRecipe = {
ScrambleEgg: { title: 'Sclamble Egg',
servings: 2,
Ingredients: ['Egg', 'Oil', 'Salt']
},Smoothies :{
title:'Smoothie',
servings: 4,
Ingredients: ['Milk','strawberry','banana'],


},CottagePie:{
title: 'CottagePie',
servings: 10,
Ingredients: ['Tender meat', 'CreamyMashedPotatoes', 'Milk']
},Roastedchicken:{
title: 'Roastedchicken',
servings: 7,
Ingredients: ['Chicken', 'Oil', 'Salt']
},MacaronniCheese:{
title: 'MacarronniCheese',
servings: 5,
Ingredients: ['Macharpnni', 'Cheese', 'Salt']
}}
for (item of favouriteRecipe){
console.log(item.)
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 exercise has made me hungry 😂


}
// write code here
12 changes: 11 additions & 1 deletion 2-mandatory/2-currency-code-lookup.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,17 @@ const COUNTRY_CURRENCY_CODES = [
];

function createLookup(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.

Great 👏

// write code here
// write code here
const newObject = new Object() ;
for (let countryCurrencyCode of countryCurrencyCodes){

let countryCode = countryCurrencyCode[1];
let country = countryCurrencyCode[0];
Comment on lines +25 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.

Again, here we can use const. It may seem that we are reassigning these variables as we loop through the array, but we aren't, we are creating new ones on each iteration. This means they are never reassigned and disappear after each loop iteration.


newObject[country] = countryCode;


}return newObject;
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Be careful with formatting here. The indentation on line 23 and this return statement should be on a new line.

}

/* ======= TESTS - DO NOT MODIFY =====
Expand Down
18 changes: 18 additions & 0 deletions 2-mandatory/3-shopping-list.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,24 @@ 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.

Nice! Another way of doing this would be to use filter:

const missingIngredients = recipe.ingredients.filter(function (item) {
  !pantryContents.includes(ingredients)
}

// write code here

const createShoppingListMissing = new Object();
const ingredientsMssing = [];
const pantryContents = pantry.fridgeContents.concat(pantry.cupboardContents);
for (let ingredient of recipe.ingredients){
if(pantryContents.includes(ingredient)===false){
ingredientsMssing.push(ingredient);
}
}
createShoppingListMissing.name = recipe.name;
createShoppingListMissing.items = ingredientsMssing;
return createShoppingListMissing;






}

/* ======= TESTS - DO NOT MODIFY =====
Expand Down
20 changes: 20 additions & 0 deletions 2-mandatory/4-restaurant.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,28 @@ const MENU = {

let cashRegister = {
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

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

},
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`
- To run all exercises/tests in the mandatory folder, run `npm test`
Expand Down
50 changes: 45 additions & 5 deletions 2-mandatory/5-writing-tests.js
Original file line number Diff line number Diff line change
Expand Up @@ -35,41 +35,81 @@ 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
*/

test.skip("a score of 71 is grade B", () => {
test("a score of 71 is grade B", () => {
/* Remove the .skip above, then write the test body. */
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 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 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
*/
test("a score of 55 is grade D", () => {
expect(convertScoreToGrade(54)).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(71)).toEqual("B");


});
61 changes: 60 additions & 1 deletion 2-mandatory/6-writing-tests-advanced.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
trainee has completed.
*/

function convertScoreToGrade() {
function convertScoreToGrade(score) {
let grade = null;

if (score >= 80) {
Expand Down Expand Up @@ -55,6 +55,17 @@ function formatCourseworkResult(trainee) {
score: 63
}
*/
test("The output of formatCourseWorkResult", () => {
let trainee = {
name: "Xin",
score: 63
}

expect(formatCourseworkResult(trainee)).toEqual("Xin's coursework was marked as grade C.");


});


/*
Write a test that checks the output of formatCourseworkResult when passed the following trainee:
Expand All @@ -63,6 +74,17 @@ function formatCourseworkResult(trainee) {
score: 78
}
*/
test("The output of formatCourseWorkResult", () => {
let trainee = {
name: "Mona",
score: 78
}

expect(formatCourseworkResult(trainee)).toEqual("Mona's coursework was marked as grade B.");


});


/*
Write a test that checks the output of formatCourseworkResult when passed the following trainee:
Expand All @@ -73,6 +95,19 @@ function formatCourseworkResult(trainee) {
subjects: ["JavaScript", "React", "CSS"]
}
*/
test("The output of formatCourseWorkResult", () => {
let trainee = {
name: "Ali",
score: 49,
age: 33,
subjects: ["JavaScript", "React", "CSS"]
}

expect(formatCourseworkResult(trainee)).toEqual("Ali's coursework was marked as grade E.");


});


/*
Write a test that checks the output of formatCourseworkResult when passed the following trainee:
Expand All @@ -81,6 +116,17 @@ function formatCourseworkResult(trainee) {
age: 29
}
*/
test("The output of formatCourseWorkResult", () => {
let trainee = {
score: 90,
age: 29
}


expect(formatCourseworkResult(trainee)).toEqual("Error: No trainee name!");


});

/*
Write a test that checks the output of formatCourseworkResult when passed the following trainee:
Expand All @@ -89,3 +135,16 @@ function formatCourseworkResult(trainee) {
subjects: ["HTML", "CSS", "Databases"]
}
*/
test("The output of formatCourseWorkResult", () => {
let trainee = {
name: "Aman",
subjects: ["HTML", "CSS", "Databases"]
}


expect(formatCourseworkResult(trainee)).toEqual("Error: Coursework percent is not a number!");


});