Lesson 2 of 10
Numbers and strings
- 11 min
- 5 checks
- 3 hints
- Go to task
Goal
Decimal division and string methods
Explanation
Ruby has two main number types: Integer for whole numbers and Float for decimals. When you divide an integer by an integer, the result is an integer too, and the fraction is dropped without any warning: 7 / 2 is 3, not 3.5. Make just one side a decimal and the division keeps its fraction, as in 7.to_f / 2, or use 7.fdiv(2) directly.
A common mistake is converting to a decimal after dividing: (47 / 6).to_f gives 7.0, because the integer division happened first and the fraction was lost before the conversion. When you display the result, round(2) rounds it to two decimal places.
A string is an object with methods of its own: strip removes spaces from both ends, upcase converts letters to upper case, reverse reverses the order of the characters, split(",") breaks the string into an array at every comma, and "-" * 20 repeats the string twenty times. None of them change the original string. Each one returns a new value, which is why you can chain them one after another, like text.strip.upcase.
Your task
In average, work out the team's average points per game as a decimal rounded to two places. Then clean up coupon and store the result in code with no spaces and in upper case, split tags at the commas and store the result in tag_list, and build divider by repeating "=" 20 times with *.
Checks
5 checks- average is a Float, not an Integer
- average is rounded to two places and equals 7.83
- code has no spaces and is in upper case
- tag_list is an array holding all three tags
- divider is made of 20 = signs