Giter Site home page Giter Site logo

ruby-looping-practice-times-atx01-seng-ft-042020's Introduction

The times Construct

Objectives

  1. Understand the times construct and how it implements looping
  2. Build a method using times

Introduction

The next construct in our list is times. There are two important distinctions to be mindful of when using times. The first one is that it has to be called on an Integer (like 1 or 10000). The second is that it executes the block a certain number of times, which is dependent on the number that it's called on. Let's look at the example below:

5.times do
  puts "Penguins like to jump off icebergs!"
end

This outputs Penguins like to jump off icebergs! five times in your Terminal.

Examples

Basic times Example: Dinner Party

Let's take a look at some more complex examples:

You just had a wildly successful dinner party with seven of your very best friends. Then they went home and left you with all of the dishes. They will not be invited back.

Let's clean those dishes using the times method:

7.times do 
  puts "I am doing the dishes left by my former friends."
end

# > "I am doing the dishes left by my former friends."
# > "I am doing the dishes left by my former friends."
# > "I am doing the dishes left by my former friends."
# > "I am doing the dishes left by my former friends."
# > "I am doing the dishes left by my former friends."
# > "I am doing the dishes left by my former friends."
# > "I am doing the dishes left by my former friends."

# => 7 (return value)

Intermediate times Example: Crime Spree

Okay, that's fine, but all we did was print some text––what if we wanted to change the value of a variable within a loop? Let's see how that works:

You are a jewel thief who has stolen one hundred (100) very valuable jewels (you are a really good jewel thief). But now you need to unload your stolen jewels from your bag into the safe in your secret hideout!

jewels_in_bag = 100

3.times do 
  puts "Hiding 10 stolen jewels."
  jewels_in_bag = jewels_in_bag - 10
end

# => 3 (return value)

puts "We have #{jewels_in_bag} jewels still to hide!"


# > "Hiding 10 stolen jewels."
# > "Hiding 10 stolen jewels."
# > "Hiding 10 stolen jewels."

# > "We have 70 jewels still to hide!"

# => nil (return value)

Advanced times Example

This is fun and all, but so far we've only printed text within the block of code within the loop. What if we wanted to do something more, say, keep track of the number of jewels we are hiding as we hide them?

jewels_in_bag = 100

3.times do 
  puts "Hiding 10 stolen jewels."
  jewels_in_bag = jewels_in_bag - 10
  puts "Now there are only #{jewels_in_bag} jewels left to hide!"
end
# => 3 (return value)

puts "We have #{jewels_in_bag} jewels still to hide!"
# => nil (return value)

# > "Hiding 10 stolen jewels."
# > "Now there are only 90 jewels left to hide!"
# > "Hiding 10 stolen jewels."
# > "Now there are only 80 jewels left to hide!"
# > "Hiding 10 stolen jewels."
# > "Now there are only 70 jewels left to hide!"

# > "We have 70 jewels still to hide!"

Using the times method

We're still struggling to master that levitation charm. Since we need to keep practicing, let's write some code that allows us to puts the phrase "Wingardium Leviosa" only seven times (instead of infinitely, like when we used just a plain loop construct).

Instructions

  1. Fork and clone this lab.
  2. Run the test suite with the learn command.
  3. You'll be coding your solution in times.rb:
  • Fill out the content of the using_times method so that calling it will puts the desired phrase, "Wingardium Leviosa" seven times by using the times keyword.

View The times Construct on Learn.co and start learning to code for free.

View Times on Learn.co and start learning to code for free.

ruby-looping-practice-times-atx01-seng-ft-042020's People

Contributors

aenbar avatar aviflombaum avatar damianb1451 avatar markedwardmurray avatar maxwellbenton avatar sarogers avatar snags88 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  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.