Giter VIP home page Giter VIP logo

ruby-iteration-practice-enumerators-quiz's Introduction

Enumerator Coding Challenge

Objectives

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

???

Code Challenge

?: Using .each

Let's try out the enumerator methods we just learned. Refer back to the previous lesson 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 .each method to enumerate over the array and append an "!" ("exclamation mark") to each menu item. You can use the << ("shovel operator") on each menu item string to add an "!" like this: "pizza" << "!".

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

Which piece of code will achieve the desired result?

( )

lunch_menu << "!"

(x)

lunch_menu.each do |lunch_item|
  lunch_item << "!"
end

( )

lunch_menu.each do |lunch_item|
  "#{lunch_item}!"
end

( )

lunch_menu.each do |lunch_item|

?: Using .collect

Below we have a variable, nums, set equal to an array of numbers. Enumerate over the array with the .collect method 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 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

( )

odds_and_evens.select do |n|
  n % 2
end

(x)

odds_and_evens.select do |n|
  n.even?
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 delete_if

Below we have a variable, cats_and_dogs, set equal to an array of strings that are either a "cat" or a "dog". We all know that cats and dogs don't get along. Iterate over the array and delete the elements that represent dogs.

cats_and_dogs = ["cat", "cat", "dog", "cat", "dog", "dog"]

Which piece of code will achieve the desired result?

(x)

cats_and_dogs.delete_if do |pet|
  pet == "dog"
end

( )

cats_and_dogs.delete_if do |pet|
  pet == "cat"
end

( )

cats_and_dogs.delete_if do |pet|
  pet == pet
end

( )

cats_and_dogs.delete_if do |pet|
  "dog"
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)

?: Using any?

Below we have a variable, quiet_and_loud, that is set equal to an array of strings. Use the .any? method to iterate over the array to determine if any of the words contained there are loud (upcased).

quiet_and_loud = ["hi", "HI", "shhh", "WHAT?!"]

Which piece of code will achieve the desired result?

( )

quiet_and_loud.any? { |w| w.upcase }

( )

quiet_and_loud.any? { |w| w.upcase? }

(x)

quiet_and_loud.any? do |word|
  word == word.upcase
end

( )

quiet_and_loud.any? do |w|
  w
end

???

ruby-iteration-practice-enumerators-quiz's People

Contributors

annjohn avatar ga-be avatar jamcelr avatar jmburges avatar markedwardmurray avatar maxwellbenton avatar nancyhawa avatar nancyko avatar pletcher avatar psmathers avatar rrcobb avatar sarogers avatar sophiedebenedetto 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

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.