-
-
Save carols10cents/b48cbd7cc45c225dfba8 to your computer and use it in GitHub Desktop.
fizzbuzz to illustrate conditionals
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Make these tests pass by filling in conditionals where the blanks currently are! | |
// Scroll down for hints :) | |
pub fn fizz_buzz(i: i32) -> String { | |
__________ { | |
"FizzBuzz".to_string() | |
} ___________ { | |
"Fizz".to_string() | |
} ___________ { | |
"Buzz".to_string() | |
} __________ { | |
i.to_string() | |
} | |
} | |
#[cfg(test)] | |
mod tests { | |
use super::*; | |
#[test] | |
fn three_is_fizz() { | |
assert_eq!("Fizz", fizz_buzz(3)); | |
} | |
#[test] | |
fn five_is_buzz() { | |
assert_eq!("Buzz", fizz_buzz(5)); | |
} | |
#[test] | |
fn fifteen_is_fizzbuzz() { | |
assert_eq!("FizzBuzz", fizz_buzz(15)); | |
} | |
#[test] | |
fn two_is_two() { | |
assert_eq!("2", fizz_buzz(2)); | |
} | |
} | |
// This program takes an integer and returns a string-- if the integer is divisible by | |
// 3, return "Fizz", divisible by 5 means to return "Buzz", and if the integer is divisible | |
// by both 3 and 5, return "FizzBuzz". For all other nubers, return them converted to a string. | |
// Conditionals in Rust are done using `if`, `else if`, and `else`. A function you might want to | |
// use is `%`, called the "mod operator"-- it returns the remainder from a division operator. | |
// When a number is divisible evenly by another number, the mod operation returns 0. |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment