Skip to content

Commit

Permalink
Create Control Structures
Browse files Browse the repository at this point in the history
  • Loading branch information
imsny authored May 7, 2024
1 parent b215d7d commit 4de0ad1
Showing 1 changed file with 51 additions and 0 deletions.
51 changes: 51 additions & 0 deletions Control Structures
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
// SPDX-License-Identifier: MIT
pragma solidity 0.8.17;

contract ControlStructures {
// Define custom errors for use within the contract
error AfterHours(uint256 time);
error AtLunch();

// Function to determine the response based on the input number
function fizzBuzz(uint256 _number) public pure returns (string memory response) {
// Check if the number is divisible by both 3 and 5
if (_number % 3 == 0 && _number % 5 == 0) {
return "FizzBuzz"; // Return "FizzBuzz" if divisible by both 3 and 5
}
// Check if the number is divisible by 3
else if (_number % 3 == 0) {
return "Fizz"; // Return "Fizz" if divisible by 3
}
// Check if the number is divisible by 5
else if (_number % 5 == 0) {
return "Buzz"; // Return "Buzz" if divisible by 5
}
// If none of the above conditions are met
else {
return "Splat"; // Return "Splat" if none of the conditions are met
}
}

// Function to determine the response based on the input time
function doNotDisturb(uint256 _time) public pure returns (string memory result) {
// Ensure the input time is within valid bounds (less than 2400)
assert(_time < 2400);

// Check different time ranges and return appropriate responses or revert with errors
if (_time > 2200 || _time < 800) {
revert AfterHours(_time); // Revert with custom error if it's after 10:00 PM or before 8:00 AM
}
else if (_time >= 1200 && _time <= 1299) {
revert AtLunch(); // Revert with custom error if it's between 12:00 PM and 1:00 PM
}
else if (_time >= 800 && _time <= 1199) {
return "Morning!"; // Return "Morning!" if it's between 8:00 AM and 11:59 AM
}
else if (_time >= 1300 && _time <= 1799) {
return "Afternoon!"; // Return "Afternoon!" if it's between 1:00 PM and 5:59 PM
}
else if (_time >= 1800 && _time <= 2200) {
return "Evening!"; // Return "Evening!" if it's between 6:00 PM and 10:00 PM
}
}
}

0 comments on commit 4de0ad1

Please sign in to comment.