Git Product home page Git Product logo

countdown-to-midnight's Introduction

Countdown to Midnight

Objectives

  1. Practice building a while loop
  2. Practice using the subtract-and-assign operator (-=)β€”the inverse of the add-and-assign operator (+=).

Review

On while Loops

This lab is going to test your skills in writing while loops. Remember, a while loop will execute your block of code only while your defined conditional evaluates as true.

For example, this script:

x = 1
while x < 10
  puts "#{x} is less than 10"
  x += 1
end

Will print this:

1 is less than 10
2 is less than 10
3 is less than 10
4 is less than 10
5 is less than 10
6 is less than 10
7 is less than 10
8 is less than 10
9 is less than 10

And return nil.

String Interpolation

Using the #{} is called interpolation. In this case, it's going to actually print out the value of x. If we just wrote puts "x is less than 10" it would print out the letter 'x' instead of the number x is representing.

The Add-And-Assign Operator (+=)

This is a shorthand useful for incrementing, or "stepping up", values. It's an operator that adds the submitting value to the value of x. In the example above x begins with a value of 1, and is incremented by 1 each time the while loop runs. The line x += 1 is the same as the line x = x + 1.

The loop is going to stop executing as soon as x hits 10, since that was the condition that we set.

The Subtract-And-Assign Operator (-=)

We can also use the subtract-and-assign operator (-=) which instead subtracts the submitted value from the given variable and reassigns that variable to the resulting difference.

Instructions

  1. Fork and clone this lab.
  2. Open it in your IDE and run the test suite. You'll be coding your solution in countdown.rb
  3. Write a method that takes in an integer argument and uses a while loop to countdown from that integer to 1, outputting "#{number} SECOND(S)!" in each iteration of the loop. The method should return "HAPPY NEW YEAR!" after the loop finishes. Hint: In Ruby, a method will return the very last line of code that it executes.
  4. Our Ruby program executes so quickly that it doesn't actually count down at the speed of one second per number. See if you can make the loop pause for one second each trip around (hint). Write this in a new method called countdown_with_sleep that also takes one integer argument for the countdown.

countdown-to-midnight's People

Contributors

ahimmelstoss avatar annjohn avatar aviflombaum avatar deniznida avatar drakeltheryuujin avatar fislabstest avatar fs-lms-test-bot avatar ihollander avatar irmiller22 avatar kthffmn avatar lizbur10 avatar markedwardmurray avatar matbalez avatar maxwellbenton avatar psmathers avatar sammarcus avatar sarogers avatar sophiedebenedetto avatar victhevenot avatar

Stargazers

 avatar

Watchers

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

countdown-to-midnight's Issues

clarify readme

remove tags, add obejctives

make instructions more explicity

add subheadings

Lab won't test

Hello, every time I type learn in this lab to check my code solution the only thing that comes up is two lines in the terminal, the first just says LOOPS and the second line just says #countdown
image

Ruby files don't execute in the terminal

Mac OS 10.12.6, I installed everything according to flatiron schools 'manual setup', but when I start the terminal, I get an error of 'Could not source '/Users/robertpeck/.rvm/scripts/version' as file does not exist. RVM will likely not work as expected.'
Plus, when I create files from the terminal ('touch math.rb', 'atom math.rb', the entire program, written in the Learn IDE is 'puts 1 + 2'), typing 'ruby math.rb' in the terminal outputs nothing.

I'm sure its a simple fix, but I don't know how to fix it. Help!

Yield and Blocks Code Along Tests Do Not Match Content

The tests in the hello_spec.rb file for this lesson do not match the content of the lesson itself.

Specifically, the lesson says this:

lessoncontent

But the hello_spec.rb file that is used to test the lesson contains 3 different tests that do not appear to be mentioned anywhere in the lesson:

rspec

Another student was able to share the hello_spec.rb file that was included when he did the lesson and updating mine to match that one was enough to get the lesson to pass, but I believe either the lesson or the test file should be updated.

In regards to the "Hint"

I noticed on several occasions to get help it mentions to use google or in this case sends you to stack overflow. That's how I usually would find out my answers as well but I wouldn't always remember or actually learn it. I think many newbies (myself included) don't know how to read the documentation and if they did, they would be able to learn how to find it and learn it without relying on someone elses answers. For example in this case the "Hint" could take them to: http://ruby-doc.org/core-1.8.7/Kernel.html#method-i-sleep

Or maybe add a section at the beginning of RUBY on how to read the docs to find what your looking for. As newbies, the documentation always seems intimidating. Just my 2 cents.

Loving the course and appreciate it very much!

debugging issue!

Hi! I defined in my file method countdown(number) and method has a loop inside. I"m trying to debug it with irb and it gives me a flowing error.
2.3.1 :001 > help

Enter the method name you want to look up.
You can use tab to autocomplete.
Enter a blank line to exit.

countdown(7)
Nothing known about .countdown(7)

More tests

These tests don't really exercise what the code should be doing. We can do better by looking at standard out for certain messages.

You can pass these tests with

def countdown(count)
  "0 left. HAPPY NEW YEAR!"
end

Countdown to Midnight Thoughts

This lab was useful as a practice in a countdown/subtraction scenario as well as an introduction to the sleep method.

However I don't think that making us create the new method countdown_with_sleep that accepts an argument of at least 5 seconds was the best way of achieving this. I passed the learn test with the following code:

def countdown_with_sleep(x)
  sleep x
end


def countdown(number)
  while number > 0
    puts "#{number} SECOND(S)!"
    number -= 1
  end
  return "HAPPY NEW YEAR!"
end

I think that students should have been instructed to code as follows:

def countdown(number)
  while number > 0
    puts "#{number} SECOND(S)!"
    number -= 1
    sleep 1
  end
  return "HAPPY NEW YEAR!"
end

While it may be confusing at first for students who are just being introduced to Ruby I think that this approach, doing away with an extra method and inserting the sleep program into the countdown(number) method itself, is more in keeping with the overarching point of readability and near intuitive nature of reading Ruby code. We've already been introduced to gets.chomp this cool extra function, sleep doesn't seem like too much of a burden content-wise this unit.

Furthermore it achieves the true spirit of the instruction that reads (in #4):

"Our Ruby program executes so quickly that is doesn't actually count down at the speed of one per second per number. See if you can make the loop pause for one second each trip around(hint)"

Countdown to 0

the instructions say to countdown to 0. But the Learn test wants you to stop at 1.

returns vs. puts "Happy New Year!"

This lesson was confusing, and felt like an unnecessary trick question. It doesn't make sense to count down to "Happy New Year", but then NOT output "Happy New Year!" Everything in my codes was working except "return" instead of "puts", and I spent an hour trying to get them to pass the test.

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.