Giter VIP home page Giter VIP logo

apples-and-holidays's Introduction

Apples and Holidays

Objectives

Get comfortable interating over arrays and hashes. This lab is broken into two sections: apple picker and holiday supplies.

Apple Picker

Instructions

In lib/apple.rb write two methods that will pick the apples out of the fruits array, one using collect, and the other using select.

fruits = ["apple", "orange", "apple"]

apple_picker_with_select(fruits) #=> ["apple", "apple"]
  1. First, code the solution to the apple_picker_with_select method. Use .select to iterate over a given array, select the items in the array that are equal to "apple" and return a new array containing all of the "apples" from the original array. Use binding.pry to understand the return value of using the .select method on an array.

  2. Now, code the solution to apple_picker_with_collect.

  • Set a variable, result_of_collecting equal to the return value of calling the .collect method on a given array to achieve the same result as above. Use the same code as in the do end block that follows .collect as you did for .select.
  • Use binding.pry to examine the return value of doing so. You should see that the return value is an array of "apple" strings, interspected with nil values. Oh no! Now we have to remove the nil elements from our array!
  • Use the .compact method on result_of_collecting to remove all the nil elements from your array.

Important: Think about the difference between select and collect. Which one makes more sense to use? What do you think the benefits are of using one over the other, in this case?

Reminder: You can require 'pry' on the top of your file and use binding.pry inside your methods. Then, when you run rspec, you'll be dropped right into the middle of the method. This will help you explore what is going on and understand how to get the tests passing.

The Holiday Suppliers

Instructions

You have a bunch of decorations for various holidays organized by season.

holiday_supplies = {
  :winter => {
    :christmas => ["Lights", "Wreath"],
    :new_years => ["Party Hats"]
  },
  :summer => {
    :fourth_of_july => ["Fireworks", "BBQ"]
  },
  :fall => {
    :thanksgiving => ["Turkey"]
  },
  :spring => {
    :memorial_day => ["BBQ"]
  }
}

Write your methods in lib/holiday.rb; use the comments in each method as guides.

  • Write a method that returns the second supply for the Fourth of July. For eg:
def second_supply_for_fourth_of_july(holiday_supplies)
  holiday_supplies[:summer][:fourth_of_july][1]
end
  • Write a method that adds a supply to both Winter holidays.

  • Write a method that adds a supply to Memorial Day.

  • Write a method that adds a new holiday and its associated supplies to any season.

  • Write a method to collect all Winter supplies from all the winter holidays. For eg:

winter_supplies(holiday_supplies) #=> ["Lights", "Wreath", etc]
  • Write a method that uses a loop to list out all the supplies you have for each holiday and the season. Use string manipulation to get your output to match what the test is expecting.

  • Here are a few helpful tips:

    • Our hash keys are symbols. We need to convert them into strings. Use the .to_s method on a symbol to convert it into a string.
    • Look closely at the output string that the test is expecting. You'll notice that it expects holiday names, like "New Years", to have both words capitalized. Ruby has a .capitalize method that you can call on a string. But, note:
      • .capitilize returns the capitalized string but doesn't change the original string. So, when you call on that same string in the future, it isn't capitalized!. You can capitalize a string for now and evermore by using the !, bang operator.
      • You'll need to capitalize both words in a given holiday's name. If you call "new years".capitalize!. It will return "New years". In order to capitalize both words, you'll need to .split the string into an array and iterate over that array to .capitalize! each word in it. Then, you'll need to .join the array back into a string.
      • If you're unfamiliar with the methods mentioned above, look them up in the Ruby documentation.

Example of expected output:

Winter:
  Christmas: Lights, Wreath
  New Years: Party Hats
  • Write a method to collect all holidays with BBQ. The method should behave as seen below:
holidays_with_bbqs(holiday_supplies)
#=> [:fourth_of_july, :memorial_day]

Reminder: This is a challenging lab, so remember to use Pry, googling and the Learn community to help you get the tests passing.

Resources

apples-and-holidays's People

Contributors

ahimmelstoss avatar arelenglish avatar aviflombaum avatar benjagross avatar deniznida avatar fislabstest avatar fs-lms-test-bot avatar gilmoursa avatar irmiller22 avatar jmburges avatar kthffmn avatar loganhasson avatar matthewkrey avatar msuzoagu avatar roseweixel avatar sarogers avatar sophiedebenedetto avatar tsiege avatar victhevenot avatar

Watchers

 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.