Skip to content
This repository was archived by the owner on Jan 3, 2023. It is now read-only.

Js core2/week3/denis p #68

Open
wants to merge 8 commits into
base: master
Choose a base branch
from
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
2 changes: 2 additions & 0 deletions Week-1/Homework/mandatory/1-writers.js
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,8 @@ let writers = [
}
];

writers.filter(item=> item.alive).map(item => console.log(`Hi, my name is ${item.firstName} ${item.lastName}. I am ${item.age} years old, and work as a ${item.occupation}.`))

/*
If you want an extra challenge, only `console.log()` the writers that are alive.
*/
3 changes: 3 additions & 0 deletions Week-1/Homework/mandatory/2-water-bottle.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,15 @@ let bottle = {
volume: 0,
fill: function() {
// calling this function should make you bottles volume = 100;
return this.volume = 100;
},
drink: function() {
// calling this function should decrease your bottles volume by 10;
return this.volume -= 10;
},
empty: function() {
// this function should return true if your bottles volume = 0
this.volume = 0 ? true : this.volume;
}
};

Expand Down
7 changes: 7 additions & 0 deletions Week-1/Homework/mandatory/3-groceries.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,3 +10,10 @@ let groceryList = {
item2: "",
item3: ""
};

groceryList.item1 = 'Potatoes';
groceryList.item2 = 'Orange Juice';
groceryList.item3 = 'Rice';

groceriesToBuy = Object.values(groceryList);
console.log(groceriesToBuy)
9 changes: 8 additions & 1 deletion Week-1/Homework/projects/1-recipes.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,4 +22,11 @@ cocoa

**/

let recipes = {};
let recipes = {
name: 'Cinnamon porridge oats with banana and berries',
servings: 1,
ingredients: ['almond milk', 'oats', 'cinnamon', 'wallnuts', 'flax seeds', 'banana', 'berries']
};


console.log(`${recipes.name} \n Serves: ${recipes.servings} \n Ingredients: \n ${recipes.ingredients.map(item=> item)}`);
25 changes: 24 additions & 1 deletion Week-1/Homework/projects/2-reading-list.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,4 +25,27 @@ If you read it, log a string like 'You already read "The Hobbit" by J.R.R. Tolki

**/

let books = [];
let books = [
{
title: 'The Chimp Paradox',
author: 'Steve Peters',
alreadyRead: true
},
{
title: 'The China study',
author: 'T. Colin Campbell',
alreadyRead: true
},
{
title: "A Connecticut Yankee in King Arthur's Court",
author: 'Mark Twain',
alreadyRead: false
},
{
title: 'Don Quixote',
author: 'Miguel de Cervantes',
alreadyRead: false
},
];

books.map(item => item.alreadyRead ? console.log(`You already read '${item.title}' by ${item.author}`) : console.log(`You still need to read '${item.title}' by ${item.author}`));
11 changes: 9 additions & 2 deletions Week-1/InClass/A-objects-intro/exercise-part-0.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,14 @@
/*

Describe your own laptop as a JavaScript object

Try to think of as many properties as you can!

*/
*/


let laptop = {
name: 'Toshiba',
color: 'white',
stickers: true,
usbSPort: 2,
}
17 changes: 9 additions & 8 deletions Week-1/InClass/A-objects-intro/exercise-part-2.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,17 +5,18 @@ The objects below have some syntax issues - try and fix them all!
*/

let kitten = {
fur colour: "orange",
age "23"
'fur colour': "orange",
age: "23"
};

let laptop =
brand: "Lenovo"
ram "5GB"
let laptop ={
brand: "Lenovo",
ram: "5GB"
}

let phone = {
operating system "iOS",
'operating system': "iOS",
hasStylus: true,
megapixels 12
"batteryLife": "24 hours"
megapixels: 12,
"batteryLife": "24 hours"
}
4 changes: 3 additions & 1 deletion Week-1/InClass/B-objects-get-set/exercise-1.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,9 @@ let kitten = {
// YOUR CODE GOES BELOW HERE



console.log(kitten.ageMonths);
console.log(kitten.isFemale);
console.log(kitten.furColour);



Expand Down
10 changes: 5 additions & 5 deletions Week-1/InClass/B-objects-get-set/exercise-2.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,13 @@
*/

let phone = {
brand: 'iPhone,
model 'iPhone X'
brand: 'iPhone',
model: 'iPhone X',
launchYear: 2017,
is Unlocked: true
;
isUnlocked: true
};

let phoneBrand = phone.bbrand;
let phoneBrand = phone.brand;
let phoneLaunchYear = phone[launchYear];

// DO NOT MODIFY BELOW THIS LINE
Expand Down
4 changes: 3 additions & 1 deletion Week-1/InClass/B-objects-get-set/exercise-3.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,9 @@
*/

// WRITE CODE BELOW THIS

let kitten = {
name: 'Gilbert'
}
// WRITE CODE ABOVE THIS

console.log(kitten.name);
Expand Down
3 changes: 2 additions & 1 deletion Week-1/InClass/B-objects-get-set/exercise-4.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,8 @@ let dog = {

// WRITE CODE BELOW THIS LINE


dog.name = 'Rex';
dog.wantsToPlay = true;

// WRITE CODE ABOVE THIS LINE

Expand Down
4 changes: 3 additions & 1 deletion Week-1/InClass/C-more-complex-objects/exercise-1.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,9 @@ let house = {
// - change the previous owners of "house" to ["Brian M.", "Fiona S."]
// - change the last name of the current owner of "house" to "Montgomery"


house.address = '51 Berkley Road';
house.previousOwners = ["Brian M.", "Fiona S."];
house.currentOwner.lastName = 'Montgomery';
/*
DO NOT EDIT ANYTHING BELOW THIS LINE
*/
Expand Down
4 changes: 3 additions & 1 deletion Week-1/InClass/C-more-complex-objects/exercise-2.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,9 @@ let newCurrentOwner = {
// - give the house a new property called 'isForSale' with the value 'false'



house.currentOwner = newCurrentOwner;
house.previousOwners[1] = 'Stephen B.';
house.isForSale = false;

/*
DO NOT EDIT ANYTHING BELOW THIS LINE
Expand Down
9 changes: 6 additions & 3 deletions Week-1/InClass/C-more-complex-objects/exercise-3.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,17 +32,20 @@ let parkAvenueHouse = {

// returns the full name (first name + last name) of the owner of the house
function getOwnerFullName(house) {

return `${house.currentOwner.firstName} ${house.currentOwner.lastName}`;
}

// returns an array of the owners' email addresses of the two houses
function getEmailAddresses(house1, house2) {

return [house1.currentOwner.email, house2.currentOwner.email]
}

// returns the address for the cheapest house out of the two
function getCheapestAddress(house1, house2) {

if(house1.price < house2.price){
return house1.address;
}
return house2.address;
}


Expand Down
5 changes: 4 additions & 1 deletion Week-1/InClass/D-methods/exercise-1.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,10 @@ Add a method "greet" so this person can say hello.

let person = {
name: "Alice",
age: 25
age: 25,
greet: function(){
return `Hello everybody.`;
}
};


Expand Down
5 changes: 4 additions & 1 deletion Week-1/InClass/D-methods/exercise-2.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,10 @@ Hint: use 'this' keyword to access the name property.

let person = {
name: "Alice",
age: 25
age: 25,
sayName: function(){
return `My name is ${this.name}`;
}
};


Expand Down
8 changes: 4 additions & 4 deletions Week-1/InClass/D-methods/exercise-3.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,11 @@ let person = {
name: "Alice",
age: 25,
currentAddress: "Glasgow",
changeAddress: (newAddress) {
currentAddress = newAddress;
changeAddress: function(newAddress) {
this.currentAddress = newAddress;
},
celebrateBirthday: function {
that.age = that.age + 1;
celebrateBirthday: function() {
this.age = this.age + 1;
}
};

Expand Down
5 changes: 4 additions & 1 deletion Week-1/InClass/D-methods/exercise-4.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,10 @@ Define a method "makeFriend" to add a new friend to her list.

let person = {
name: "Alice",
friends: ["John", "Nina"]
friends: ["John", "Nina"],
makeFriend: function(newFriend){
return this.friends.push(newFriend);
}
};


Expand Down
7 changes: 7 additions & 0 deletions Week-1/InClass/D-methods/exercise-5.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,17 @@ let coffeeMachine = {
},
insertedAmount: 0,
insertMoney: function (amount) {
this.insertedAmount += amount;

},
getCoffee: function (coffee) {

if(this.prices[coffee] <= this.insertedAmount){
this.insertedAmount = 0;
return `Please take your ${coffee}`;
}
return `Sorry you don't have enough money for a ${coffee}`;

}
};

Expand Down
6 changes: 3 additions & 3 deletions Week-1/InClass/E-arrays-of-objects/exercise-1.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,11 +25,11 @@ WRITE YOUR CODE BELOW
*/


var persons = // Complete here
var persons = [person1, person2, person3] // Complete here

var personNames = // Complete here
var personNames = persons.map(item => item.name) // Complete here

var personsYoungerThan28YearsOld = // Complete here
var personsYoungerThan28YearsOld = persons.filter(item=> item.age < 28) // Complete here


/*
Expand Down
7 changes: 4 additions & 3 deletions Week-1/InClass/E-arrays-of-objects/exercise-2.js
Original file line number Diff line number Diff line change
Expand Up @@ -40,11 +40,12 @@ WRITE YOUR CODE BELOW
*/


let destinationNamesWithin500Kms = // Complete here
let destinationNamesWithin500Kms = travelDestinations.filter(item => item.distanceKms < 500).map(item=> item.destinationName); // Complete here
// console.log(destinationNamesWithin500Kms)

let destinationNameReachableByFerry = // Complete here
let destinationNameReachableByFerry = travelDestinations.filter(item => item.transportations.includes('ferry')).map(item => item.destinationName)// Complete here

let destinationNamesMoreThan300KmsAwayByTrain = // Complete here (PRINT THE RESULT IN THE CONSOLE USING FOREACH)
let destinationNamesMoreThan300KmsAwayByTrain = travelDestinations.filter(item => item.transportations.includes('train') && item.distanceKms > 300).map(item=> item.destinationName) // Complete here (PRINT THE RESULT IN THE CONSOLE USING FOREACH)


/*
Expand Down
8 changes: 8 additions & 0 deletions Week-1/InClass/E-arrays-of-objects/exercise-3.js
Original file line number Diff line number Diff line change
Expand Up @@ -61,12 +61,20 @@ let restaurantFinderApplication = {
restaurants: restaurants,
findAvailableRestaurants: function (numberOfPeople) {
// Complete here
let availableRestaurants = restaurants.filter(res=> numberOfPeople <= res.totalSeats - res.numberOfCustomers).map(res=> res.name);
return availableRestaurants;
},
findRestaurantServingDish: function (dishName) {
// Complete here
let customRestaurant = restaurants.filter(res => res.menu.includes(dishName)).map(res => res.name);
return customRestaurant;

},
countNumberOfRestaurantsInArea: function (area) {
// Complete here
let count = 0;
let countRestaurant = restaurants.filter(res => res.address.area.includes(area)).map(res => count++ )
return count;
}
};

Expand Down
4 changes: 2 additions & 2 deletions Week-1/InClass/F-object-keys/exercise-part-0.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,8 @@ let highScores = {

// ONLY EDIT BELOW HERE

let capitalCitiesKeys = ;
let highScoresKeys;
let capitalCitiesKeys = Object.keys(capitalCities) ;
let highScoresKeys = Object.keys(highScores);

// ONLY EDIT ABOVE HERE

Expand Down
4 changes: 2 additions & 2 deletions Week-1/InClass/F-object-keys/exercise-part-1.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,9 @@ let mentorsAges = {

// ONLY EDIT BELOW THIS LINE

let mentorsNames = ;
let mentorsNames = Object.keys(mentorsAges);

let mentorsNamedUppercased = ;
let mentorsNamedUppercased = mentorsNames.map(item => item.toUpperCase());

// ONLY EDIT ABOVE THIS LINE

Expand Down
6 changes: 3 additions & 3 deletions Week-1/InClass/F-object-keys/exercise-part-2.js
Original file line number Diff line number Diff line change
Expand Up @@ -35,14 +35,14 @@ let storeBranches = {

// # 1
// prints [ 'glasgow', 'edinburgh' ]
console.log()
console.log(Object.keys(storeBranches))

// # 2
// prints [ 'manager', 'assistant', 'interns' ]
console.log()
console.log(Object.keys(storeBranches.glasgow))

// # 3
// prints [ 'head_intern', 'intern' ]
console.log()
console.log(Object.keys(storeBranches.edinburgh.interns))

// ONLY EDIT ABOVE THIS LINE
Loading