Giter Site home page Giter Site logo

ruby-enumerables-prework-quiz-atlanta-web-071519's Introduction

Enumerator Quiz

Learning Goal

Become familiar with using common iterators introduced in the previous lesson.

???

Challenge

?: Using .each

Let's try out the enumerator methods we just learned. Refer back to the previous lessons to help you pass this challenge. Below, we have a variable, lunch_menu, set equal to an Array of lunch menu items.

Since you're super-hungry and super-excited about lunch, use the .map method to enumerate over the Array and append an "!" ("exclamation mark") to each menu item.

lunch_menu = ["pizza", "sandwich", "sushi", "soup", "salad"]

Which piece of code will achieve the desired result?

( )

lunch_menu.reduce{|memo, item| memo << item }

(x)

lunch_menu.map{|item| "#{item}!" }

( )

lunch_menu.reduce{|item, memo| memo += item }

( )

lunch_menu.map{|item| (item, "!") }

?: Using .collect

Below we have a variable, nums, set equal to an Array of numbers. Enumerate over the Array and return a new Array of the squares of those numbers.

nums = [1, 2, 3, 4]

Which piece of code will achieve the desired result?

(x)

nums.collect { |n| n * n }

( )

nums.collect do |n|
  n + n
end

( )

nums.collect { |n| nn }

( )

nums.collect do |n|
  n
end

?: Using .select

Below we have a variable, odds_and_evens, set equal to an Array of numbers. Use the .select enumerator to iterate over the Array and return any even numbers. This requires checking if a number is even. If you're unsure how to do that, reference the Ruby Documentation for Integer or try a Google search!

odds_and_evens = [1, 3, 2, 18, 5, 10, 24]

Which piece of code will achieve the desired result?

( )

odds_and_evens.select do |n|
  n / 2
end

(x)

odds_and_evens.select do |n|
  n % 2 == 0
end

( )

odds_and_evens.select do |n|
  n.odd?
end

( )

odds_and_evens.select do |n|
  n + 2
end

?: Using .find

Below we once again have a variable, odds_and_evens, set equal to an Array of numbers. This time, use the .find method to iterate over the Array and return only the first Array element that is odd.

odds_and_evens = [2, 3, 2, 18, 5, 10, 24]

Which piece of code will achieve the desired result?

( )

odds_and_evens.find do |num|
  num.even?
end

( )

odds_and_evens.find do |num|
  num / 2
end

( )

odds_and_evens.find do |num|
  num
end

(x)

odds_and_evens.find do |num|
  num.odd?
end

?: Using include?

Below we have a variable, famous_cats, set equal to an Array of famous cats. Use the .include? method to check and see if the Array includes the string "Maru".

famous_cats = ["Maru", "Lil Bub", "Grumpy Cat"]

Which piece of code will achieve the desired result?

(x)

famous_cats.include?("Maru")

( )

famous_cats.include?

( )

famous_cats.include?(true)

???

View Enumerator Coding Challenge on Learn.co and start learning to code for free.

ruby-enumerables-prework-quiz-atlanta-web-071519's People

Contributors

maxwellbenton avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.