This file contains hidden or 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
let obj = { | |
key1: "value1", | |
key2: "value2", | |
key3: "value3" | |
} | |
let items = Object.values(obj); | |
console.log(items); | |
// ["value1", "value2", "value3"] | |
items.map(value => { | |
console.log(value) |
This file contains hidden or 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
let obj = { | |
key1: "value1", | |
key2: "value2", | |
key3: "value3" | |
} | |
let items = Object.keys(obj); | |
console.log(items); | |
// ["key1", "key2", "key3"] | |
items.map(key => { | |
let value = obj[key]; |
This file contains hidden or 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
let obj = { | |
key1: "value1", | |
key2: "value2", | |
key3: "value3" | |
} | |
for (let key in obj) { | |
let value = obj[key]; | |
console.log(key, value); | |
} | |
// key1 value1 |
This file contains hidden or 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
// 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); |
This file contains hidden or 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
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) | |
} |
This file contains hidden or 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
// 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); | |
}); |
This file contains hidden or 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 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'); | |
} |
This file contains hidden or 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
function interest(principal, rate, years) { | |
rate = rate || 3.5; | |
years = years || 5; | |
return (principal * rate) / 100 * (years) | |
} | |
console.log(interest(10000)); | |
This file contains hidden or 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
function sum(discount, ...prices) { | |
const total = prices.reduce((a, b) => a + b); | |
return total * (1 - discount); | |
} | |
console.log(sum(0.1, 20, 30, 1)); |
This file contains hidden or 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
// Function Declaration | |
function walk() { | |
console.log('walk'); | |
} | |
// Named Function Expression | |
let run1 = function walk() { | |
console.log('run1'); | |
}; |