This file contains 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
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}" |
This file contains 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
class Animal | |
attr_accessor :name | |
def initialize(name) | |
@name = name | |
end | |
def eat(other) | |
puts "#{@name} ate #{other.name}! #{self.noise}" | |
end |
This file contains 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
class Shape | |
attr_accessor :color | |
def initialize(color = nil) | |
@color = color || 'Red' | |
end | |
def larger_than?(other) | |
self.area >= other.area | |
end |
This file contains 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
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 |
This file contains 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
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" |
This file contains 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
class Array | |
def new_map | |
new_array = [] | |
self.each do |item| | |
new_array << yield(item) | |
end | |
new_array | |
end | |
def new_map!(&block) |
This file contains 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
'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' }; |
This file contains 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
'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 |
This file contains 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
'use strict'; | |
var _ = require('underscore'); | |
//######### EXAMPLE 1 #################/ | |
//use _.reduce to multiply all the values in an array | |
var arr = [10,20,40,50,60,70]; | |
var multiplied = _.reduce(arr, function (x, y) { return x * y; }); | |
//tests |
This file contains 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
'use strict'; | |
/**************************************** | |
* Chapter 3 Exercises: Functions | |
***************************************** | |
#EXERCISE 1: Minimum | |
The previous chapter introduced the standard function Math.min that returns | |
its smallest argument. We can do that ourselves now. Write a function min that |
OlderNewer