Skip to content

Commit 52add02

Browse files
author
Anna Aitchison
committed
Add code for the objects workshop
1 parent 0ebf3fd commit 52add02

6 files changed

Lines changed: 156 additions & 0 deletions

File tree

‎objects/100.js‎

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
// Predict and explain...
2+
// What will be logged by this code?
3+
// Check your prediction and explanation by running the code.
4+
5+
const person1 = {
6+
"name": "Abdi",
7+
"location": "London",
8+
"id_number": 17,
9+
};
10+
11+
const person2 = {
12+
"name": "Shadi",
13+
"job": "Software Engineer",
14+
"location": "London",
15+
"id_number": 28,
16+
};
17+
18+
const person3 = person2;
19+
20+
person3.location = "Manchester";
21+
22+
console.log(person1.name);
23+
console.log(person2["name"]);
24+
console.log(person1.id_number > person2["id_number"]);
25+
console.log(person1.job);
26+
console.log(person1.location === person2.location);
27+
console.log(person1.location === person3.location);
28+
console.log(person2.location === person3.location);

‎objects/200.js‎

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
// Predict and explain...
2+
// What will happen when this program is run?
3+
// Check your prediction and explanation by running the program.
4+
// Fix anything that needs fixing.
5+
6+
const person = {
7+
"name": "Jemima",
8+
"location",
9+
"id_number" = 9,
10+
};
11+
12+
console.assert(person.name === "Jemima");
13+
console.assert(person.location === "Glasgow");
14+
console.assert(person.id_number === 9);

‎objects/300.js‎

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
// Predict and explain...
2+
// What will happen when this program is run?
3+
// Check your prediction and explanation by running the program.
4+
// Fix anything that needs fixing.
5+
6+
function checkLivesNearCYF(person) {
7+
const cyfLocations = ["Birmingham", "Cape Town", "Glasgow", "London", "Manchester"];
8+
return cyfLocations.includes(person.location);
9+
}
10+
11+
const mo = {
12+
"name": "Mo",
13+
"city": "Glasgow",
14+
"focus": "React",
15+
};
16+
17+
const sayed = {
18+
"name": "Sayed",
19+
"city": "Edinburgh",
20+
"focus": "SQL",
21+
}
22+
23+
console.assert(checkLivesNearCYF(mo));
24+
console.assert(!checkLivesNearCYF(sayed));

‎objects/400.js‎

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
// Predict and explain...
2+
// What will happen when this program is run?
3+
// Check your prediction and explanation by running the program.
4+
// Fix anything that needs fixing.
5+
6+
function printSaladRecipe(forPerson) {
7+
const saladRecipe = {
8+
"name": "salad",
9+
"ingredients": ["lettuce", "corn", "carrots", "cucumber"],
10+
"rating_out_of_10": 8,
11+
"steps": [
12+
"Shred the lettuce",
13+
"Cut the carrots into small pieces",
14+
"Slice the cucumber",
15+
"Mix all the vegetables together in a bowl",
16+
]
17+
};
18+
19+
if (forPerson.eatsMeat) {
20+
saladRecipe.push("chicken");
21+
saladRecipe.steps.push("Mix in the chicken");
22+
}
23+
24+
console.log(`For ${forPerson.name} to make ${saladRecipe.name}:`);
25+
console.log("Get:");
26+
for (const ingredient of saladRecipe.ingredients) {
27+
console.log(` * ${ingredient}`);
28+
}
29+
for (const step of saladRecipe.steps) {
30+
console.log(step);
31+
}
32+
}
33+
34+
const person1 = {
35+
"name": "Ola",
36+
"eatsMeat": true,
37+
};
38+
39+
const person2 = {
40+
"name": "Steve",
41+
"eatsMeat": false,
42+
};
43+
44+
printSaladRecipe(person1);
45+
console.log("");
46+
printSaladRecipe(person2);

‎objects/500.js‎

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
// Fill in the code below so that the tests pass.
2+
3+
const people = [];
4+
5+
people.push({
6+
"name": "Saqib",
7+
"favourite_food": "salad",
8+
});
9+
10+
people.push({
11+
"name": "Shadi",
12+
"favourite_food": "mango",
13+
});
14+
15+
people.push({
16+
"name": "Navid",
17+
"favourite_food": "macarons",
18+
});
19+
20+
const favouriteFoods = [];
21+
22+
// TODO: Fill the array favouriteFoods with the favourite foods of each person.
23+
// You must not type the strings (i.e. your solution should not involve you typing the word "salad").
24+
25+
console.assert(favouriteFoods.length === 3);
26+
console.assert(favouriteFoods.includes("salad"));
27+
console.assert(favouriteFoods.includes("mango"));
28+
console.assert(favouriteFoods.includes("macarons"));

‎objects/600.js‎

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
// Predict and explain...
2+
// What will happen when this program is run?
3+
// Check your prediction and explanation by running the program.
4+
// Fix anything that needs fixing.
5+
6+
const person = {
7+
"name": "Manu",
8+
"favourite_ice_cream": "vanilla",
9+
"favourite_topping": "marshmallows",
10+
};
11+
12+
function assertFieldEquals(object, field, targetValue) {
13+
console.assert(object.field === targetValue);
14+
}
15+
16+
assertFieldEquals(person, "favourite_ice_cream", "vanilla");

0 commit comments

Comments
 (0)