Skip to content

Instantly share code, notes, and snippets.

Ian DesJardins iandesj

Block or report user

Report or block iandesj

Hide content and notifications from this user.

Learn more about blocking users

Contact Support about this user’s behavior.

Learn more about reporting abuse

Report abuse
View GitHub Profile
@iandesj
iandesj / enum.js
Created Nov 27, 2019
Example of an Enum-esque construct in JS
View enum.js
const enumValue = (name) => Object.freeze({toString: () => name});
const Colors = Object.freeze({
RED: enumValue("Colors.RED"),
BLUE: enumValue("Colors.BLUE"),
GREEN: enumValue("Colors.GREEN")
});
@iandesj
iandesj / lcm.py
Last active Oct 23, 2019
Least common multiple finder i wrote in python... cuz who doesn't need a good LCM finder from time to time, amirite?
View lcm.py
def lcm(nums):
greatest = max(nums)
while True:
all_good = all(
list(map(lambda x: greatest % x == 0, nums))
)
if all_good:
return greatest
greatest += 1
@iandesj
iandesj / object_destructuring_examples.js
Last active Nov 14, 2019
Examples of object destructuring vs direct object key-value access
View object_destructuring_examples.js
const taxPayer = {
firstName: 'Ian', lastName: 'DesJardins',
ssn: '123-45-6789', pastDue: true,
};
console.log('Object destructuring example');
// destructure the taxPayer object into new variables
const { firstName, lastName, ssn, pastDue } = taxPayer;
console.log('firstName =', firstName);
@iandesj
iandesj / array_destructuring_examples.js
Last active Nov 14, 2019
Examples of array destructuring vs array index accessing
View array_destructuring_examples.js
const taxPayer = [ 'Ian', 'DesJardins', '123-45-6789', true ];
console.log('Array destructuring example');
// destructure the taxPayer array into new variables
const [ firstName, lastName, ssn, pastDue ] = taxPayer;
console.log('firstName =', firstName);
console.log('lastName =', lastName);
console.log('ssn =', ssn);
console.log('pastDue =', pastDue, '\n');
@iandesj
iandesj / array_from_examples.js
Last active Nov 14, 2019
Examples of Array.from() vs for loop
View array_from_examples.js
const year = 2019;
const month = 10; // november, zero-indexed
const startDay = 14;
console.log('Array.from() example');
const dateList = Array.from({length:7}, (val, idx) => {
return new Date(year, month, startDay + idx);
});
console.log('dateList = ', dateList, '\n');
@iandesj
iandesj / reduce_examples.js
Last active Nov 14, 2019
Examples of reduce() vs for loop
View reduce_examples.js
const animals = [
{animal: 'zebra', count: 4}, {animal: 'tiger', count: 2},
{animal: 'lion', count: 3}, {animal: 'turtle', count: 9},
{animal: 'pig', count: 2},
];
console.log('reduce() example');
const count = animals.reduce((total, curr) => {
return total += curr.count;
}, 0);
@iandesj
iandesj / map_examples.js
Last active Nov 14, 2019
Examples of map() vs for loop
View map_examples.js
const fruits = [ 'pineapple', 'apple', 'banana', 'pear' ];
console.log('map() example');
const upperCaseFruits = fruits.map((value) => {
return value.toUpperCase();
});
console.log('upperCaseFruits =', upperCaseFruits, '\n');
@iandesj
iandesj / filter_examples.js
Last active Nov 14, 2019
Examples of filter() vs for loop
View filter_examples.js
const fruits = [ 'pineapple', 'apple', 'banana', 'pear' ];
const matchedFruits = fruits.filter((element) => {
return element.includes('apple');
});
let matchedFruitsAgain = [];
for (let i = 0; i < fruits.length; i++) {
if (fruits[i].includes('apple')) {
@iandesj
iandesj / find_example.js
Last active Nov 14, 2019
Examples of find() vs for loop
View find_example.js
const fruits = ['pineapple', 'apple', 'banana','pear'];
const aFruit = fruits.find((element) => {
return element.includes('apple');
});
let aFruit;
for (let i = 0; i < fruits.length; i++) {
if (fruits[i].includes('apple')) {
@iandesj
iandesj / foreach_example.js
Created Oct 11, 2019
Examples of foreach() vs for loop
View foreach_example.js
const fruits = [ 'pineapple', 'apple', 'banana', 'pear' ];
// improved looping
fruits.forEach((value, index, array) => {
console.log('fruit at index', index, '=', value);
});
// what you may already be used to
for (let i=0; i<fruits.length; i++) {
console.log('fruit at index', i, '=', fruits[i]);
You can’t perform that action at this time.