Skip to content

Instantly share code, notes, and snippets.

@kironroy
kironroy / maxAndReduce.js
Created June 15, 2023 20:12
max and reduce
// 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);
@kironroy
kironroy / reduce.js
Created June 15, 2023 20:04
reduce array
const movements = [200, 450, -400, 3000, -650, -130, 70, 1300];
const balance = movements.reduce((acc, cur) => acc + cur, 0);
console.log(balance);
@kironroy
kironroy / filterarray.js
Last active June 15, 2023 18:55
filter array
// 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);
'use strict';
// Data
// forEach
const info1 = {
owner: 'Sita Troy',
movements: [200, 450, -400, 3000, -650, -130, 70, 1300],
interestRate: 1.2, // %
pin: 1111,
@kironroy
kironroy / maparray.js
Last active June 15, 2023 18:31
map array, arrow function
// 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) =>
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)}`);
}
@kironroy
kironroy / gist:c0e749a537c8216e3884ee060ef67776
Created June 12, 2023 21:12
First-class functions vs Higher-order functions
// 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);
@kironroy
kironroy / higherorder.js
Last active May 27, 2023 01:36
higher order function
// 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!!! ☝️`);
@kironroy
kironroy / stringChallenge.js
Created May 26, 2023 01:52
string challenge (sort of)
// 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
@kironroy
kironroy / strings.js
Last active May 25, 2023 22:11
strings js
'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';