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

Week3 patrick #71

Open
wants to merge 22 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
3 changes: 3 additions & 0 deletions .vscode/settings.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"liveServer.settings.port": 5501
}
11 changes: 11 additions & 0 deletions Week-1/Homework/extra/extra-homework.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,17 @@ Click "ATTEMPT" to test your solution.
Exercises:

- [Fix my Method](https://www.codewars.com/kata/558710234f02dcc4a8000005)
function myFunction() {
var myObject = {
objProperty: "string",
objMethod: function() {
return myObject.objProperty;
}
}

return myObject;
};

- [Regular Ball Super Ball](https://www.codewars.com/kata/53f0f358b9cb376eca001079/train/javascript)

## Reading
Expand Down
23 changes: 23 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,29 @@ let writers = [
}
];

// https://reactgo.com/javascript-loop-through-array-of-objects/#:~:text=In%20es6%20we%20have%20a,over%20the%20array%20of%20objects.&text=forEach%20methods%20takes%20the%20callback,object%20present%20in%20the%20array.
// writers.forEach((writer) => console.log(writer.firstName, writer.lastName)); --> to test if this forEach method works

// METHOD1: forEach methods takes the callback function as an argument and runs on each object present in the array.
writers.forEach((writer) => console.log(`Hi, my name is ${writer.firstName} ${writer.lastName}. I am ${writer.age} years old, and work as a ${writer.occupation}.`));

// METHOD2: in this for of loop, on each iteration different object is assigned to the writer variable.
for(let writer of writers) {
console.log(`Hi, my name is ${writer.firstName} ${writer.lastName}. I am ${writer.age} years old, and work as a ${writer.occupation}.`);
}

/*
If you want an extra challenge, only `console.log()` the writers that are alive.
*/

// https://alligator.io/js/filter-array-method/
function aliveWriter(writer) {
if (writer.alive === true) {
return writer;
}
}

let aliveWriters = writers.filter(aliveWriter);
// console.log(aliveWriters); ---> to check if the output is correct.

aliveWriters.forEach((writer) => console.log(`Hi, my name is ${writer.firstName} ${writer.lastName}. I am ${writer.age} years old, and work as a ${writer.occupation}.`));
7 changes: 7 additions & 0 deletions Week-1/Homework/mandatory/2-water-bottle.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,20 @@ We made a start on this for you here:
let bottle = {
volume: 0,
fill: function() {
return this.volume = 100;
// calling this function should make you bottles volume = 100;
},
drink: function() {
// calling this function should decrease your bottles volume by 10;
if (this.volume > 10) {
return this.volume -= 10;
} return this.volume = 0;
},
empty: function() {
// this function should return true if your bottles volume = 0
if (this.volume === 0) {
return true;
}
}
};

Expand Down
12 changes: 9 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,13 @@
let groceriesToBuy = [];

let groceryList = {
item1: "",
item2: "",
item3: ""
item1: "Potatoes",
item2: "Orange Juice",
item3: "Rice"
};

// https://www.javascripttutorial.net/object/convert-an-object-to-an-array-in-javascript/#:~:text=To%20convert%20an%20object%20to%20an%20array%20you%20use%20one,entries()%20.

groceriesToBuy = Object.values(groceryList);

console.log(groceriesToBuy);
65 changes: 65 additions & 0 deletions Week-1/Homework/mandatory/4-codewars.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,72 @@ Click "ATTEMPT" to test your solution.
Exercises:

- [Training JS #5: Basic data types--Object](https://www.codewars.com/kata/571f1eb77e8954a812000837/train/javascript)
function animal(obj){
return `This ${obj.color} ${obj.name} has ${obj.legs} legs.`
}

- [Welcome!](https://www.codewars.com/kata/welcome/train/javascript)
function greet(language) {
var database = {
english: 'Welcome',
czech: 'Vitejte',
danish: 'Velkomst',
dutch: 'Welkom',
estonian: 'Tere tulemast',
finnish: 'Tervetuloa',
flemish: 'Welgekomen',
french: 'Bienvenue',
german: 'Willkommen',
irish: 'Failte',
italian: 'Benvenuto',
latvian: 'Gaidits',
lithuanian: 'Laukiamas',
polish: 'Witamy',
spanish: 'Bienvenido',
swedish: 'Valkommen',
welsh: 'Croeso'
};
for (var key in database) {
if(key == language) {
return database[key];
}
}
return database['english'];
}

function greet(language) {
return languages[language] || languages['english'];
}

var languages = {
'english': 'Welcome',
'czech': 'Vitejte',
'danish': 'Velkomst',
'dutch': 'Welkom',
'estonian': 'Tere tulemast',
'finnish': 'Tervetuloa',
'flemish': 'Welgekomen',
'french': 'Bienvenue',
'german': 'Willkommen',
'irish': 'Failte',
'italian': 'Benvenuto',
'latvian': 'Gaidits',
'lithuanian': 'Laukiamas',
'polish': 'Witamy',
'spanish': 'Bienvenido',
'swedish': 'Valkommen',
'welsh': 'Croeso'
}

- [Crash Override](https://www.codewars.com/kata/crash-override/train/javascript)
function aliasGen(first, last){
first = first[0].toUpperCase()
last = last[0].toUpperCase()
if (!first.match(/[A-Z]/) || !last.match(/[A-Z]/)){
return "Your name must start with a letter from A - Z."
}
return `${firstName[first]} ${surname[last]}`
}

- [Job Matching #1](https://www.codewars.com/kata/56c22c5ae8b139416c00175d/train/javascript)
- [Split the Bill](https://www.codewars.com/kata/5641275f07335295f10000d0/train/javascript)
17 changes: 15 additions & 2 deletions Week-1/Homework/projects/1-recipes.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,5 +21,18 @@ cumin
cocoa

**/

let recipes = {};
//https://stackoverflow.com/questions/54851645/how-to-display-both-key-and-value-in-object-using-javascript/54851680

let recipes = {
Title: 'Mole',
Serves: 2,
Ingredients: [
'cinnamon',
'cumin',
'cocoa',
]
};

for (let key in recipes) {
console.log(key, recipes[key]);
}
42 changes: 41 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,44 @@ If you read it, log a string like 'You already read "The Hobbit" by J.R.R. Tolki

**/

let books = [];
//Exercise 1 -> the question asks to loop through the array. My solution only targets index 0. How would I loop should the array have more than 1 object?

let books = [
{title: 'The Hobbit',
author: 'J.R.R. Tolkien',
alreadyRead: false
}
];
let exercise1 - books.map(x => `${x.title} by ${x.author}`).toString();
let bookAndAuthor = `"${books[0].title}" by ${books[0].author}`;

console.log(bookAndAuthor);

//Exercise 2 -> I tried to create a function to insert into the forEach function but not sure how to continue with it. Or if there's a better way to formulate my function?

function hasAlreadyRead() {
if (books.alreadyRead === true) {
return `You already read ${bookAndAuthor}`;
} else {
return `You still need to read "The Lord of the Rings" by J.R.R. Tolkien.`;
}
}

books.forEach(hasAlreadyRead());


let exercise2 = books.map(x => x.alreadyRead === true ? `You already read ${x.title} by ${x.author}` : `You still need to read $${x.title} by ${x.author}`).toString();














11 changes: 10 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,13 @@ Describe your own laptop as a JavaScript object

Try to think of as many properties as you can!

*/
*/

let laptop = {
name: 'Toshiba',
color: 'black',
OS: 'Windows 10',
numberOfUSB: 2,
webcam: true,
touchscreen: false
};
37 changes: 37 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 @@ -7,3 +7,40 @@ Assign each of them to a separate variable

*/

let house = {
number: 88,
wall: 'brick',
roof: 'tiles',
yearBuilt: 1930,
driveway: true,
garden: true
};

let pet = {
species: 'mouse',
name: 'Blacky',
age: 1.5,
sex: 'male',
hasOffsprings: false
}

let car = {
make: 'Toyota',
model: 'Yaris',
year: 2015,
color: 'white'
validMOT: true
}

let plant = {
type: 'tomato',
color: 'green',
heightInCm: 50,
}

let artClass = {
courseLeader: 'Mrs Smith'
teacher: 'Mr Murphy',
classSize: 20,
includesFieldTrips: true
}
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"
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"
}
15 changes: 14 additions & 1 deletion Week-1/InClass/A-objects-intro/exercise-part-3.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,4 +13,17 @@ Make sure you use words like:
- declaring a variable
- etc.

*/
*/

/*
1. Below is a variable with an object literal.
2. The variable is laptop.
2. There are 3 properties in the object literal: brand, ram and hasTouchscreen.
3. Values are Lenovo [a string], 5GB [a string], false [a boolean] respectively.
*/

let laptop = {
brand: "Lenovo",
ram: "5GB",
hasTouchscreen: false
}
2 changes: 1 addition & 1 deletion Week-1/InClass/B-objects-get-set/exercise-1.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ let kitten = {

// YOUR CODE GOES BELOW HERE


console.log(kitten);



Expand Down
12 changes: 6 additions & 6 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: '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
Loading