This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// reduce method is most powerful array method in JavaScript | |
// maximum value with reduce | |
const max = movements.reduce((acc, mov) => { | |
if (acc > mov) return acc; | |
else return mov; | |
}, movements[0]); | |
console.log(max); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const movements = [200, 450, -400, 3000, -650, -130, 70, 1300]; | |
const balance = movements.reduce((acc, cur) => acc + cur, 0); | |
console.log(balance); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// filter method | |
// push in JS to make code more functional | |
// also more practical application: | |
// method chaining! | |
const movements = [200, 450, -400, 3000, -650, -130, 70, 1300]; | |
const deposits = movements.filter(mov => mov > 0); | |
console.log(movements); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
'use strict'; | |
// Data | |
// forEach | |
const info1 = { | |
owner: 'Sita Troy', | |
movements: [200, 450, -400, 3000, -650, -130, 70, 1300], | |
interestRate: 1.2, // % | |
pin: 1111, |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// map is different than forEach | |
// forEach creates side effects | |
// map returns each of the strings | |
// gets added to a new array | |
// maps have no side-effects | |
const movements3 = [200, 450, -400, 3000, -650, -130, 70, 1300]; | |
const movementDesc = movements3.map( | |
(mov, i) => |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const movements = [200, 450, -400, 3000, -650, -130, 70, 1300]; | |
// for | |
for (const [i, movement] of movements.entries()) { | |
if (movement > 0) { | |
console.log(`Movement ${i + 1}: You deposited ${movement}`); | |
} else { | |
console.log(`Movement ${i + 1}: You withdrew ${Math.abs(movement)}`); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// First-Class Functions | |
// functions are simlply values | |
// functions are just another 'type' of object | |
// Higher-Order Functions | |
// A function that recevies another function as | |
// an argument, that returns a new function, or, both | |
// const greet = () => console.log('Hi Vita'); | |
// btnClose addEventListener('click', greet); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// higher order function, strings example | |
const capitalize = function (str) { | |
return str.toUpperCase(); | |
}; | |
console.log(capitalize('rita roy')); | |
const yo = function (str, fx) { | |
console.log(`Original String: ${str}`); | |
console.log(`Transformed ${fx(str)}, Yo!!! ☝️`); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// data | |
const firstName = 'first_name'; | |
const someVariable = 'Some_Variable'; | |
const calculateAge = 'calculate_AGE'; | |
const delayedDeparture = 'delayed_departure'; | |
// my var below, check it it works | |
const foo_no = 'FOO NO' | |
// array |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
'use strict'; | |
// Data needed for a later exercise | |
const flights = | |
'_Delayed_Departure;fao93766109;txl2133758440;11:25+_Arrival;bru0943384722;fao93766109;11:45+_Delayed_Arrival;hel7439299980;fao93766109;12:05+_Departure;fao93766109;lis2323639855;12:30'; | |
const airline = 'TAP Air Portugal'; | |
const plane = 'A320'; |