Skip to content

Instantly share code, notes, and snippets.

View sevperez's full-sized avatar

Severin Perez sevperez

View GitHub Profile
var myNums = [1, 2, 3, 4, 5];
function doubleNum(num) {
return num * 2;
}
// Built-in Array.prototype.map function, using anonymous function argument
var doubledNums = myNums.map(function(num) {
return num * 2;
});
function applicator(fn, val) {
return function() {
fn(val);
};
}
function speak(string) {
console.log(string);
}
function speak(string) {
console.log(string);
}
var delayedFunction = function(fn) {
return function(val, delay) {
setTimeout(function() {
fn(val);
}, delay);
};
function isPalindrome(str) {
var reverse = str.split("").reverse().join("");
return str === reverse;
}
function getCharCounts(chars) {
var charCounts = {};
for (var i = 0, charLen = chars.length; i < charLen; i += 1) {
if (charCounts[chars[i]]) {
charCounts[chars[i]] += 1;
var prices = [400, 80, 375, 870];
// 1
for (var i = 0; i < prices.length; i += 1) {
console.log(prices[i]);
}
// logs 400, 80, 375, 870
// 2
for (var k in prices) {
var products = {
"widget": 400,
"gear": 80,
"crank": 375,
"lever": 870,
};
// 1
var productKeys = Object.keys(products);
for (var i = 0; i < productKeys.length; i += 1) {
var prices = [400, 80, 375, 870];
var products = {
"widget": 400,
"gear": 80,
"crank": 375,
"lever": 870,
};
var pricesDescriptors = Object.getOwnPropertyDescriptors(prices);
var prices = [400, 80, 375, 870];
Object.defineProperty(prices, 1, { enumerable: false });
for (var k in prices) {
console.log(prices[k]);
}
// logs 400, 375, 870 ... the 80 is missing!
var products = {
"widget": 400,
"gear": 80,
"crank": 375,
"lever": 870,
};
Object.defineProperty(products, "gear", { enumerable: false });
// 1
var products = {
"widget": 400,
"gear": 80,
"crank": 375,
"lever": 870,
};
var otherProducts = Object.create(products);
otherProducts["wheel"] = 210;