Skip to content

Commit

Permalink
Merge pull request: New challenge: If else #7
Browse files Browse the repository at this point in the history
  • Loading branch information
dcodesdev authored Jun 1, 2024
2 parents d7585be + be5fe93 commit 0e37e22
Show file tree
Hide file tree
Showing 9 changed files with 71 additions and 0 deletions.
4 changes: 4 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ members = [
"challenges/animal-sanctuary-registry",
"challenges/median-and-mode",
"challenges/graceful-error-handling",
"challenges/if-else",

"crates/cli",
]
Expand Down
12 changes: 12 additions & 0 deletions challenges/challenges.json
Original file line number Diff line number Diff line change
Expand Up @@ -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"
}
]
6 changes: 6 additions & 0 deletions challenges/if-else/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
[package]
name = "if-else"
version = "0.1.0"
edition = "2021"

[dependencies]
22 changes: 22 additions & 0 deletions challenges/if-else/description.md
Original file line number Diff line number Diff line change
@@ -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);
```
9 changes: 9 additions & 0 deletions challenges/if-else/src/lib.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
mod tests;

pub fn is_even(n: i32) -> bool {
if n % 2 == 0 {
true
} else {
false
}
}
3 changes: 3 additions & 0 deletions challenges/if-else/src/starter.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
pub fn is_even(n: i32) -> bool {
// Your code here...
}
13 changes: 13 additions & 0 deletions challenges/if-else/src/tests.rs
Original file line number Diff line number Diff line change
@@ -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);
}
}
1 change: 1 addition & 0 deletions challenges/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ enum Difficulty {
#[allow(non_camel_case_types)]
enum Track {
RUST_BASICS,
CONTROL_FLOW,
}

#[derive(Deserialize)]
Expand Down

0 comments on commit 0e37e22

Please sign in to comment.