Require Conditions
Part of the Fundamentals section of Coddy's Solidity journey. Lesson 46 of 53.
Use require(condition, message) to reject a call when an expected condition is false. A successful condition lets execution continue. A failed requirement reverts the call and its state changes. Validate an input before using it in an operation that depends on that condition.
The statements in this excerpt run inside the provided main function.
uint256 quantity = 7;
require(quantity > 0, "Positive quantity required");
console.log(quantity * 3);The value seven passes the positive-quantity check, so the calculation prints twenty-one. If quantity were zero, the call would revert instead. The message explains the failed requirement; it is not printed during a successful call.
require continues when its condition is true and reverts when it is false.
Challenge
EasyRequire divisor to be greater than zero with a useful error message. Then print total divided by divisor. The visible output tests use positive divisors; your code must still include the guard.
The tests pass arguments to main in this order: uint256 total, uint256 divisor. Each test starts with fresh contract state.
Use console.log for the requested output. Print only the requested values, one per line, with no extra labels unless the task specifies them.
Try it yourself
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.26;
import "forge-std/console.sol";
contract Main {
function main(uint256 total, uint256 divisor) external view {
// Write your solution here.
}
}This lesson includes a short quiz. Start the lesson to answer it and track your progress.
All lessons in Fundamentals
Practice on your own: Solidity playground