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

Js core 2/week 3/berhane #75

Open
wants to merge 16 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
6 changes: 6 additions & 0 deletions Week-1/Homework/mandatory/1-writers.js
Original file line number Diff line number Diff line change
Expand Up @@ -42,3 +42,9 @@ let writers = [
/*
If you want an extra challenge, only `console.log()` the writers that are alive.
*/
for(let objs=0; objs<writers.length;objs++){
if(writers[objs].alive===true){
//console.log(writers.)
console.log(`Hi, my name is ${writers[objs].firstName} ${writers[objs].lastName}.I am ${writers[objs].age} years old, and work as a ${writers[objs].occupation }`)
}
}
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;
this.volume = 100
},
drink: function() {
// calling this function should decrease your bottles volume by 10;
this.volume -= 10
},
empty: function() {
// this function should return true if your bottles volume = 0
this.volumev = 0
}
};

Expand Down
11 changes: 8 additions & 3 deletions Week-1/Homework/mandatory/3-groceries.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,12 @@
let groceriesToBuy = [];

let groceryList = {
item1: "",
item2: "",
item3: ""
item1: "Potatoes",
item2: "Orange Juice",
item3: "Rice"
};
for (let [keys,value] of Object.entries(groceryList)) {
groceriesToBuy.push(value)
}

console.log(groceriesToBuy)
12 changes: 11 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,14 @@ cocoa

**/

let recipes = {};
let recipes = {
title: "Banana Bread",
Serves: 2,
Ingredients: ["Banana","Flour","Egg","Milk"]
};
/*Object.entries(recipes).forEach(([key, value]) => {
console.log();})*/
console.log(recipes.title)
console.log(`Serves: ${recipes.Serves}`)
console.log("Ingredients:")
console.log(recipes.Ingredients + "\n")
21 changes: 20 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,23 @@ If you read it, log a string like 'You already read "The Hobbit" by J.R.R. Tolki

**/

let books = [];
let books = [
{ title:"Atomic Habits",
author:"James CLean",
alreadyRead: true},
{
title: "12 Rules of Life",
author: "Jordan B Peterson",
alreadyRead: false},
{
title: "Philosophy Who needs it",
author: "Ayn Rand",
alreadyRead: true
}];
for (let i = 0; i<books.length;i++){
if(books[i].alreadyRead===true){
console.log(`You already read ${books[i].title} by ${books[i].author}`)
}else{
console.log(`You still need to read ${books[i].title} by ${books[i].author}`)
}
}
10 changes: 9 additions & 1 deletion Week-1/InClass/A-objects-intro/exercise-part-0.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,12 @@ Describe your own laptop as a JavaScript object

Try to think of as many properties as you can!

*/
*/
let myLaptop ={
screenSize: "14 inch",
processr: "AMD",
ram: "8 GB",
storage:"1TB"

}
console.log(myLaptop)
14 changes: 14 additions & 0 deletions Week-1/InClass/A-objects-intro/exercise-part-1.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,18 @@ can describe with a JavaScript object
Assign each of them to a separate variable

*/
let animals = {

}
let cars = {

}
let continents ={

}
let planets = {

}
let markets = {

}
16 changes: 8 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,17 @@ The objects below have some syntax issues - try and fix them all!
*/

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

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

let phone = {
operating system "iOS",
operatingsystem: "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 @@ -9,7 +9,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,14 +5,14 @@
*/

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

let phoneBrand = phone.bbrand;
let phoneLaunchYear = phone[launchYear];
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: "Glibert"
}
// 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 @@ -8,7 +8,8 @@ let dog = {
};

// WRITE CODE BELOW THIS LINE

dog.name = "Rex"
dog.wantsToPlay = true


// WRITE CODE ABOVE THIS LINE
Expand Down
3 changes: 3 additions & 0 deletions Week-1/InClass/C-more-complex-objects/exercise-1.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,11 @@ let house = {
*/

// - change the address of "house" to '51 Berkley Road'
house.address = '51 Berkley Road'
// - change the previous owners of "house" to ["Brian M.", "Fiona S."]
house.previousOwners = ["Brian M.", " Fiona S."]
// - change the last name of the current owner of "house" to "Montgomery"
house.currentOwner.lastName = "Montgomery"


/*
Expand Down
5 changes: 5 additions & 0 deletions Week-1/InClass/C-more-complex-objects/exercise-2.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,13 @@ let newCurrentOwner = {
*/

// - assign the value of the variable 'newCurrentOwner' as the value to the house's "currentOwner"
house.currentOwner = newCurrentOwner
// - from the list of previous owners, replace only "John A." with "Stephen B."
house.previousOwners[1] = " Stephen B."

// - give the house a new property called 'isForSale' with the value 'false'
house.isForSale = false




Expand Down
15 changes: 11 additions & 4 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,24 @@ 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) {

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) {

let cheapest = Math.min(house1.price, house2.price)
if(cheapest === house1.price){
return house1.address
}
else{
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,10 +6,13 @@ Add a method "greet" so this person can say hello.

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

};



/*
DO NOT EDIT ANYTHING BELOW THIS LINE
*/
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(){
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(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(addfriend){
return this.friends +="," + addfriend
}
};


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

},
getCoffee: function (coffee) {
if(this.insertedAmount >= 2.40 && this.insertedAmount< 3.00){
return 'Please take your cappuccino'
}
else if(this.insertedAmount >= 1.50 && this.insertedAmount < 2.40){
return 'Please take your blackCoffee'
}
else if(this.insertedAmount >= 4.00){
return 'Please take your flatWhite'
}
else if(this.insertedAmount >3.00 && this.insertedAmount < 4.00){
return 'Sorry you don\'t have enough money for a flatWhite'
}

}
};
Expand All @@ -38,6 +51,6 @@ console.log(`Expected result: 'Please take your blackCoffee'. Actual result: ${c
coffeeMachine.insertMoney(4.00);
console.log(`Expected result: 'Please take your flatWhite'. Actual result: ${coffeeMachine.getCoffee('flatWhite')}`);

coffeeMachine.insertMoney(2.40);
coffeeMachine.insertMoney(3.40);
console.log(`Expected result: 'Sorry you don't have enough money for a flatWhite'. Actual result: ${coffeeMachine.getCoffee('flatWhite')}`);

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(names => names.name )// Complete here

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


/*
Expand Down
6 changes: 3 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,11 @@ WRITE YOUR CODE BELOW
*/


let destinationNamesWithin500Kms = // Complete here
let destinationNamesWithin500Kms = travelDestinations.filter(distance => distance.distanceKms <= 500).map(names=>names.destinationName) // Complete here

let destinationNameReachableByFerry = // Complete here
let destinationNameReachableByFerry = travelDestinations.filter(transport=>transport.transportations.includes("ferry")).map(names => names.destinationName) // Complete here

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


/*
Expand Down
Loading