Enumerators Flashcards

You may prefer our related Brainscape-certified flashcards:
1
Q

return a new array with each value multiplied by two:

arr = [ 1, 2, 3, 4 ]

A
arr = [ 1, 2, 3, 4 ]
newArray = arr.map { |x| x * 2 }
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

what is a another name for the MAP method?

A

COLLECT

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

Return the sum of all values in the given array:

nums = [1, 2, 3, 4, 5]

A

nums = [1, 2, 3, 4, 5]
nums.inject(0) do |accum, element| # accum is initially set to 0, the method argument
accum + element
end

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

what is another name for the INJECT method?

A

REDUCE

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

Return the items from the given range that are divisible by 3:
num = ( 1..10 )

A
nums = ( 1..10 )
nums.select do | i |
  i % 3 == 0
end 
# ==> [3, 6, 9 ]
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

What is another name for the SELECT method?

A

FIND_ALL

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

nums = [1, 4, 5, 6, 7 ]

A

nums.count # ==> 5

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

Does this array contain a 4?
nums = [ 1, 4, 5, 6, 7 ]

A

nums.include?(4)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

Does the array contain even numbers?
nums = [2, 3, 5, 7 ]

A

nums.any? do |x|
x % 2 == 0
end

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

What methods would you use to determine if all or none of the collection equal a value or criteria?

A

ALL? / NONE?

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

Write a method that finds the median of a given array of integers. If the array has an odd number of integers, return the middle item from the sorted array. If the array has an even number of integers, return the average of the middle two items from the sorted array.

A

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

Create a method that takes in an Array of Strings and uses inject to return the concatenation of the strings.

A

..

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

How would you write a block given an array of positive integers and return another array with only the values over 100?

A

arr.select do |x|
x >= 100
end

How well did you know this?
1
Not at all
2
3
4
5
Perfectly