Skip to content

Commit d1a9d07

Browse files
author
Max Kelly
committed
First commit
0 parents  commit d1a9d07

38 files changed

+1785
-0
lines changed

Diff for: .vscode/settings.json

+6
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
{
2+
"cSpell.words": [
3+
"existng",
4+
"todos"
5+
]
6+
}

Diff for: advanced/arrow-function.js

+20
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
// The below is another way to perform a function. Its meant to be clean for one line uses
2+
const square = (num) => num * num
3+
console.log(square(5))
4+
5+
const people = [{
6+
name: 'Max',
7+
age: 21
8+
}, {
9+
name: 'Andrew',
10+
age: 27
11+
}, {
12+
name: 'John',
13+
age: 143
14+
}]
15+
16+
const under30 = people.filter((person) => person.age < 30)
17+
console.log(under30)
18+
19+
const whose21 = people.find((person) => person.age === 21)
20+
console.log(whose21.name)

Diff for: advanced/arrow-function2.js

+17
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
2+
// Arguments only exist in regular functions
3+
const add = function(a, b) {
4+
return arguments [0] + arguments[1]
5+
}
6+
7+
console.log(add(11, 22, 33, 44))
8+
9+
// You shouldn't also use an arrow function in the below method
10+
const car = {
11+
color: 'Red',
12+
getSummary: function () {
13+
return `The car is ${this.color}`
14+
}
15+
}
16+
17+
console.log(car.getSummary())

Diff for: advanced/conditional-operator.js

+36
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
const myAge = 2
2+
const message = myAge >= 18 ? 'You can vote!' : 'You cannot vote.'; // This is a more compact and easier way instead of doing the below if statement
3+
4+
// if (myAge >= 18) {
5+
// message = 'You can vote!'
6+
// } else {
7+
// message = 'You cannot vote'
8+
// }
9+
10+
console.log(message)
11+
12+
// -----
13+
// Conditional operator using functions
14+
const myDrinkingAge = 20
15+
16+
const showPage = () => {
17+
console.log('Showing the page')
18+
}
19+
20+
const showErrorPage = () => {
21+
console.log('Showing the error page')
22+
}
23+
24+
myDrinkingAge >= 21 ? showPage() : showErrorPage()
25+
26+
// Challenge
27+
28+
// 1. Print "team size: if less than or equal to 4"
29+
// 2. Print "Too many people on your team"
30+
31+
const team = ["Tyler", "John", "John", "John"];
32+
const teamCount = team.length <= 3 ? `Your team size is: ${team.length}` : 'Too many people on your team'
33+
34+
console.log(teamCount)
35+
36+

Diff for: advanced/truthy-falsy.js

+14
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
const products = [{name: 'Computer'}]
2+
const product = products[0]
3+
4+
// Truthy - Values that resolve to true in a boolean context
5+
// Falsy - Values that resolve to false in boolean context
6+
7+
// Falsy Values are - false, 0, empty string, null, undefined, NAN
8+
// Truthy values are - anything else
9+
10+
if (product) {
11+
console.log('Product found')
12+
} else {
13+
console.log('Product not found')
14+
}

Diff for: advanced/try-catch.js

+19
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
// The below catches errors that can occur in the site.
2+
3+
4+
const getTip = (amount) => {
5+
if (typeof amount === 'number') { // The typeof says this input must be a number.
6+
return amount * 0.25
7+
} else {
8+
throw Error('Argument must be a number') // If its not a number return this error message.
9+
}
10+
}
11+
12+
// True runs the code and if an error occurs it runs the error message above.
13+
14+
try {
15+
const result = getTip(true);
16+
console.log(result);
17+
} catch (e) {
18+
console.log(e.message)
19+
}

Diff for: advanced/type-coercion.js

+12
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
// Type Coercion - a string, a number, a boolean - Should avoid doing this.
2+
3+
console.log('5' + 5) // Number version
4+
console.log('5' - 5) // Number version
5+
console.log('5' == 5) // Double == mean that it doesn't take in the string number it sees it as a 5. You should always look at using ===.
6+
7+
// const type = typeof 'Hello' // Type of basically tells you the type of telling you if its a 'number', 'string' etc...
8+
9+
const value = true + 12
10+
const type = typeof value
11+
console.log(value)
12+
console.log(type)

Diff for: arrays/expense-tracker.js

+49
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
const account = {
2+
name: 'Max Kelly',
3+
expenses: [],
4+
income: [],
5+
balance: [],
6+
7+
addExpense: function(description, amount) {
8+
this.expenses.unshift({
9+
description: description,
10+
amount: amount
11+
})
12+
},
13+
14+
addIncome: function(description, amount) {
15+
this.income.unshift({
16+
description: description,
17+
amount: amount
18+
})
19+
},
20+
21+
getAccountSummary: function() {
22+
let totalExpenses = 0
23+
let totalIncome = 0
24+
let balance = 0
25+
26+
this.expenses.forEach(function(item){
27+
totalExpenses = totalExpenses + item.amount
28+
})
29+
30+
this.income.forEach(function(item){
31+
totalIncome = totalIncome + item.amount
32+
})
33+
34+
balance = totalIncome - totalExpenses
35+
return `${account.name} has a total of $${totalExpenses} and earns a total of $${totalIncome} making his balance: $${balance} `
36+
},
37+
38+
};
39+
40+
// Expense -> description, amount
41+
// addExpense -> This will take two arguments 1. Description 2. Amount
42+
// getAccountSummary - This function will total up all the expenses -> "Max Kelly has ${total} in expenses"
43+
44+
45+
account.addExpense('Rent', 900);
46+
account.addExpense('Food', 400);
47+
account.addIncome('Job', 3000);
48+
account.addIncome('Sale', 200);
49+
console.log(account.getAccountSummary())

Diff for: arrays/notes.js

+120
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,120 @@
1+
// const notes = [
2+
// 'Max',
3+
// 'Kelly'
4+
// ];
5+
6+
// notes.push('My new note'); // This adds a value onto the array
7+
// console.log(notes);
8+
9+
// notes.pop(notes); // This removes the last item on the array
10+
// console.log(notes);
11+
12+
// notes.shift(); // This removes the first item on the array
13+
// console.log(notes);
14+
15+
// notes.unshift('My first note'); // This adds a note onto the start of the array.
16+
// console.log(notes);
17+
18+
// console.log(notes[0].length); // Puts the length of the array.
19+
20+
//notes.splice(1, 1);
21+
// This works as a way to remove selected items
22+
// So it would say start at the 1st item and then remove the 1st item.
23+
// So it's notes.splice(start at, remove)
24+
//console.log(notes);
25+
26+
// notes.splice(1, 0, 'This is the new note' ) // This allows us to add a value in the position.
27+
// console.log(notes);
28+
29+
// notes[2] = 'This is now the new note 3';
30+
31+
// // The below function performs for every object in the array.
32+
// // item lists the actual item in the array
33+
// notes.forEach(function (item, index) {
34+
// console.log(index)
35+
// console.log(item)
36+
// });
37+
38+
// for (let count = 2; count >= 0; count--) {
39+
// console.log(count)
40+
// }
41+
42+
// for (let count = notes.length - 1; count >= 0; count--) {
43+
// console.log(notes[count])
44+
// }
45+
46+
//console.log(notes.indexOf({})); // This searches for the item in the array and returns he position back. If there isn't an object matching then it will return -1
47+
48+
49+
//--------------------
50+
51+
const notes = [
52+
{
53+
title: "ay next trip",
54+
body: " I would like to go to Spain"
55+
},
56+
{
57+
title: "Habits to work on",
58+
body: " Exercise. Eating a bit better"
59+
},
60+
{
61+
title: "Office modifications",
62+
body: "Max Kelly"
63+
}
64+
];
65+
66+
const findNote = function (notes, noteTitle) {
67+
return notes.find(function(note, index){
68+
return note.title.toLowerCase() === noteTitle.toLowerCase()
69+
})
70+
};
71+
72+
// The below allows the user to search for an object
73+
const findNotes = function (notes, query) {
74+
return notes.filter(function (note, index) {
75+
const isTitleMatch = note.title.toLowerCase().includes(query.toLowerCase())
76+
const isBodyMatch = note.body.toLowerCase().includes(query.toLowerCase())
77+
return isTitleMatch || isBodyMatch;
78+
})
79+
}
80+
//----------------
81+
82+
// Sorting the array
83+
84+
const sortNotes = function (notes) {
85+
notes.sort(function (a, b) {
86+
if (a.title.toLowerCase() < b.title.toLowerCase()) {
87+
return -1
88+
} else if (b.title.toLowerCase() < a.title.toLowerCase()) {
89+
return 1
90+
} else {
91+
return 0
92+
}
93+
})
94+
};
95+
96+
sortNotes(notes);
97+
console.log(notes);
98+
//-------------------
99+
100+
// The below calls the function findNotes and searches the ''
101+
//console.log(findNotes(notes, 'work'));
102+
// const findNote = function(notes, noteTitle) {
103+
// const index = notes.findIndex(function(note, index) {
104+
// return note.title.toLowerCase() === noteTitle.toLowerCase();
105+
// });
106+
// return notes[index];
107+
// };
108+
109+
// const note = findNote(notes, 'office modifications')
110+
// console.log(note);
111+
112+
// const index = notes.findIndex(function (note, index) {
113+
// return note.title === 'Habits to work on'
114+
// });
115+
116+
// console.log(index);
117+
118+
119+
120+

0 commit comments

Comments
 (0)