Skip to content

Instantly share code, notes, and snippets.

View basekays's full-sized avatar

Hyejin Kay Albito basekays

View GitHub Profile
var _ = {};
_.pluck = function (list, key) {
var result = [];
for (var i = 0; i<list.length; i++) {
if (list[i][key]) {
result.push(list[i][key]);
}
}
return result;
@basekays
basekays / titleCase.js
Created November 3, 2016 02:01
take a string, change the first character of each word into capitalized letter :)
function titleCase(str) {
var strLowerCase = str.toLowerCase();
var split = strLowerCase.split(" ");
for (var i = 0; i< split.length; i++) {
var firstChar = split[i].charAt(0).toUpperCase();
split[i] = firstChar + split[i].substr(1);
}
var newStr = split.join(" ");
return newStr;
//your code here!
/*
Q1. Using your knowledge of JavaScript data types (i.e. numbers, strings, booleans,
arrays and objects), represent our solar system as data (hint: the link is to a
Wikipedia article – you should use it). You can include as many attributes as you
like, but the following are required:
- Age of the Solar System
- Distance from the center of the galaxy
- Number of known comets
- The Planets
@basekays
basekays / _.each
Created January 24, 2017 19:38
_.each syntax
// Call iterator(value, key, collection) for each element of collection.
// Accepts both arrays and objects.
//
// Note: _.each does not have a return value, but rather simply runs the
// iterator function over each item in the input collection.
_.each = function(collection, iterator) {
_.each(collection, iterator(value, key, collection) {
}
@basekays
basekays / -
Created January 24, 2017 20:17
-
_.each = function(collection, iterator) {
if (Array.isArray(collection)) {
for (var i = 0; i<collection.length; i++) {
return iterator(collection[i], i, collection);
}
} else {
for (var i in collection) {
return iterator(collection[i]);
}
@basekays
basekays / reduce
Created January 24, 2017 23:18
reduce
var memoInCallback, itemInCallback;
_.reduce(['item'], function(memo, item) {
memoInCallback = memo;
itemInCallback = item;
}, 'memo');
expect(memoInCallback).to.equal('memo');
expect(itemInCallback).to.equal('item');
@basekays
basekays / full
Last active January 24, 2017 23:23
full.js
// Reduces an array or object to a single value by repetitively calling
// iterator(accumulator, item) for each item. accumulator should be
// the return value of the previous iterator call.
//
// You can pass in a starting value for the accumulator as the third argument
// to reduce. If no starting value is passed, the first element is used as
// the accumulator, and is never passed to the iterator. In other words, in
// the case where a starting value is not passed, the iterator is not invoked
// until the second element, with the first element as its second argument.
_.reduce = function(collection, iterator, accumulator) {
for (var i = 0; i < collection.length; i++) {
accumulator = iterator(accumulator, collection[i]);
}
if (accumulator === undefined) {
return collection[0];
}
return accumulator;
};
/*
_.reduce = function(collection, iterator, accumulator) {
for (var i = 0; i < collection.length; i++) {
accumulator = iterator(accumulator, collection[i]);
}
if (accumulator === undefined) {
return collection[0];
}
return accumulator;
};
var getElementsByClassName = function(className, n) {
// your code here
var resultArr = [];
var searchClass = function(element) {
if (element.classList) {
if (element.classList.contains(className)) {
resultArr.push(element);
}
}
for (var i = 0; i < element.childNodes.length; i++) {