Skip to content

Instantly share code, notes, and snippets.

@alexhawkins
alexhawkins / checkboxToggling.js
Created August 27, 2015 20:24
cool lodash code
/* checks to see if item id arrangement in packages has changed */
let compareItemIds = (oldIds, newIds) => {
return _.difference(newIds, oldIds).concat(_.difference(oldIds, newIds));
};
/* gets the item of ids of current packages and orignal packages
and compares them for changes */
let haveItemsChanged = () => {
let curIds = [];
@alexhawkins
alexhawkins / changeClass.rb
Last active August 29, 2015 13:57
Changing class and using the Each Method
class Array
def sum_numbers
sum = 0
self.each { |num| sum += num }
sum
end
def add_index
self.each_with_index do |item, index|
self[index] = "#{index} is #{item}"
@alexhawkins
alexhawkins / selfExample.rb
Last active August 29, 2015 13:57
Why self?
class Animal
attr_accessor :name
def initialize(name)
@name = name
end
def eat(other)
puts "#{@name} ate #{other.name}! #{self.noise}"
end
@alexhawkins
alexhawkins / InheritanceExample.rb
Last active August 29, 2015 13:57
Why use self.width in the area method instead of @width?
class Shape
attr_accessor :color
def initialize(color = nil)
@color = color || 'Red'
end
def larger_than?(other)
self.area >= other.area
end
@alexhawkins
alexhawkins / sortingMethod.rb
Last active August 29, 2015 13:57
Sorting
def sort_by_length(sort_this_array)
sort_this_array.sort { |x, y| x.length <=> y.length }
end
def filter(filter_this_array)
filter_this_array.select { |v| v > 5 }
end
@alexhawkins
alexhawkins / mapsortarray.rb
Last active August 29, 2015 13:57
Sort a String of Random Numbers
map_this_array = "1 2 3 19 23 234 523 323 325 53 533 234 14 78 68676 46 990 67845"
p map_this_array.split.map(&:to_i).sort{|x,y| x <=> y}.join(", ")
# yields => "1, 2, 3, 14, 19, 23, 46, 53, 78, 234, 234, 323, 325, 523, 533, 990, 67845, 68676"
@alexhawkins
alexhawkins / mapmethodmaker.rb
Last active August 29, 2015 13:57
Confused about when to use self again? This works the same without self. Also why use & instead :&
class Array
def new_map
new_array = []
self.each do |item|
new_array << yield(item)
end
new_array
end
def new_map!(&block)
@alexhawkins
alexhawkins / callbackEx.js
Last active August 29, 2015 14:04
Hack Reactor Callback Exercise Answers
'use strict';
var _ = require('underscore'); //require underscore library
/* NOTE: to use the above you need to install underscore on your computer first.
If you are using node.js, type 'npm install underscore' at the command line. */
//##############EXERCISE 1######################
var slang = ['cool', 'sick', 'dope', 'wicked', 'boss'];
var slangObj = { word1: 'cool', word2: 'sick', word3: 'dope', word4: 'wicked', word5: 'boss' };
@alexhawkins
alexhawkins / closures.js
Last active August 29, 2015 14:04
Hack Reactor Closure Exercise Answers
'use strict';
//NUMBER 1
//sets first name
var setFirstName = function(firstName){
//inner function allows us to alter last name
var fullName = function(lastName){
//note that the firstName has been saved
//for use in our inner function
@alexhawkins
alexhawkins / underscoreRemix.js
Last active August 29, 2015 14:05
Simplified Underscore .Each method
/* SIMPLIFIED UNDERSCORE .EACH method*/
var _ = function(obj) {
if (obj instanceof _) return obj;
if (!(this instanceof _)) return new _(obj);
this._wrapped = obj;
};