diff --git a/Cargo.lock b/Cargo.lock index d26684c..af8741f 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -538,6 +538,10 @@ dependencies = [ "unicode-normalization", ] +[[package]] +name = "if-else" +version = "0.1.0" + [[package]] name = "indexmap" version = "2.2.6" diff --git a/Cargo.toml b/Cargo.toml index ebec49b..0bb006f 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -10,6 +10,7 @@ members = [ "challenges/animal-sanctuary-registry", "challenges/median-and-mode", "challenges/graceful-error-handling", + "challenges/if-else", "crates/cli", ] diff --git a/challenges/challenges.json b/challenges/challenges.json index 78ce6f8..ac44970 100644 --- a/challenges/challenges.json +++ b/challenges/challenges.json @@ -106,5 +106,17 @@ "tags": [], "created_at": "2024-05-06T00:00:00Z", "updated_at": "2024-05-06T00:00:00Z" + }, + { + "id": 10, + "title": "Basic If-Else Statements", + "slug": "if-else", + "short_description": "Learn how to use if-else statements in Rust to control the flow of your program.", + "language": "RUST", + "difficulty": "EASY", + "track": "CONTROL_FLOW", + "tags": ["control flow", "if-else"], + "created_at": "2024-06-01T00:00:00Z", + "updated_at": "2024-06-01T00:00:00Z" } ] diff --git a/challenges/if-else/Cargo.toml b/challenges/if-else/Cargo.toml new file mode 100644 index 0000000..1cfca7e --- /dev/null +++ b/challenges/if-else/Cargo.toml @@ -0,0 +1,6 @@ +[package] +name = "if-else" +version = "0.1.0" +edition = "2021" + +[dependencies] diff --git a/challenges/if-else/description.md b/challenges/if-else/description.md new file mode 100644 index 0000000..ab55824 --- /dev/null +++ b/challenges/if-else/description.md @@ -0,0 +1,22 @@ +Control flow is a fundamental concept in any programming language, including Rust. The `if-else` statement is a basic control flow construct that allows you to execute different code blocks based on certain conditions. + +## Your task + +In this challenge, you will write a function `is_even(n: i32) -> bool` that checks if a given number is even or odd using an `if-else` statement. The function should return `true` if the number is even, and `false` if it is odd. + +## Requirements + +- The function should take an integer as input. +- Use an `if-else` statement to determine if the number is even. +- Return `true` if the number is even. +- Return `false` if the number is odd. + +## Example + +```rust +assert_eq!(is_even(4), true); +assert_eq!(is_even(7), false); +assert_eq!(is_even(0), true); +assert_eq!(is_even(-2), true); +assert_eq!(is_even(-3), false); +``` diff --git a/challenges/if-else/src/lib.rs b/challenges/if-else/src/lib.rs new file mode 100644 index 0000000..e3e674f --- /dev/null +++ b/challenges/if-else/src/lib.rs @@ -0,0 +1,9 @@ +mod tests; + +pub fn is_even(n: i32) -> bool { + if n % 2 == 0 { + true + } else { + false + } +} diff --git a/challenges/if-else/src/starter.rs b/challenges/if-else/src/starter.rs new file mode 100644 index 0000000..5f9997e --- /dev/null +++ b/challenges/if-else/src/starter.rs @@ -0,0 +1,3 @@ +pub fn is_even(n: i32) -> bool { + // Your code here... +} diff --git a/challenges/if-else/src/tests.rs b/challenges/if-else/src/tests.rs new file mode 100644 index 0000000..42031d3 --- /dev/null +++ b/challenges/if-else/src/tests.rs @@ -0,0 +1,13 @@ +#[cfg(test)] +mod tests { + use crate::is_even; + + #[test] + fn test_is_even() { + assert_eq!(is_even(4), true); + assert_eq!(is_even(7), false); + assert_eq!(is_even(0), true); + assert_eq!(is_even(-2), true); + assert_eq!(is_even(-3), false); + } +} diff --git a/challenges/tests.rs b/challenges/tests.rs index 8c1da40..56015ea 100644 --- a/challenges/tests.rs +++ b/challenges/tests.rs @@ -14,6 +14,7 @@ enum Difficulty { #[allow(non_camel_case_types)] enum Track { RUST_BASICS, + CONTROL_FLOW, } #[derive(Deserialize)]