Skip to content

Instantly share code, notes, and snippets.

View BolajiAyodeji's full-sized avatar
🥑
I'm looking for work. Let's talk!

Bolaji Ayodeji BolajiAyodeji

🥑
I'm looking for work. Let's talk!
View GitHub Profile
@BolajiAyodeji
BolajiAyodeji / object.js
Created March 3, 2019 14:50
Enumerating Properties of an Object
const circle = {
radius: 1,
draw() {
console.log('draw');
}
};
for (let key in circle) {
console.log(key, circle[key]);
}
@BolajiAyodeji
BolajiAyodeji / object-clone.js
Created March 3, 2019 15:00
Cloning an Object
const circle = {
radius: 1,
draw() {
console.log('draw');
}
};
// ONE
const another = {};
@BolajiAyodeji
BolajiAyodeji / function.js
Created March 3, 2019 15:12
Defining Functions
// Function Declaration
function walk() {
console.log('walk');
}
// Named Function Expression
let run1 = function walk() {
console.log('run1');
};
@BolajiAyodeji
BolajiAyodeji / rest-operator.js
Created March 3, 2019 15:33
The rest operator
function sum(discount, ...prices) {
const total = prices.reduce((a, b) => a + b);
return total * (1 - discount);
}
console.log(sum(0.1, 20, 30, 1));
@BolajiAyodeji
BolajiAyodeji / interest.js
Last active March 3, 2019 18:11
Default parameters in functions
function interest(principal, rate, years) {
rate = rate || 3.5;
years = years || 5;
return (principal * rate) / 100 * (years)
}
console.log(interest(10000));
@BolajiAyodeji
BolajiAyodeji / getters-setters.js
Last active March 3, 2019 18:45
Getters and Setters with error handling
const person = {
firstName: 'Bolaji',
lastName: 'Ayodeji',
get fullName() {
return `${person.firstName} ${person.lastName}`
},
set fullName(value) {
if(typeof value !== 'string') {
throw new Error('Value is not a string');
}
@BolajiAyodeji
BolajiAyodeji / this.js
Last active March 3, 2019 20:11
The This Keyword
// method (function inside an object) -> object
// function -> global (window, global)
const video = {
title: 'Bird Box',
tags: ['english', 'french', 'chineese'],
showTags() {
this.tags.forEach(tag => {
console.log(this.title, tag);
});
@BolajiAyodeji
BolajiAyodeji / count.js
Created March 3, 2019 20:45
Count occurence of a number in an array with Error Handling
function countOccurences(array, searchElement) {
if (!Array.isArray(array)) {
throw new Error('Enter a valid array');
}
return array.reduce((accumulator, current) => {
const occurence = (current === searchElement) ? 1 : 0;
return accumulator + occurence;
}, 0)
}
// for... in loop
let start = performance.now();
let obj = {
key1: "value1",
key2: "value2",
key3: "value3"
}
for (let key in obj) {
let value = obj[key];
console.log(key, value);
let obj = {
key1: "value1",
key2: "value2",
key3: "value3"
}
for (let key in obj) {
let value = obj[key];
console.log(key, value);
}
// key1 value1