Skip to content

Commit 1d0922c

Browse files
author
theunicorndev237
committed
loops in solidity
1 parent ab892b9 commit 1d0922c

File tree

2 files changed

+46
-0
lines changed

2 files changed

+46
-0
lines changed

Diff for: 009

Whitespace-only changes.

Diff for: 009-loops-in-solidity.sol

+46
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
// SPDX-License-Identifier: GPL-3.0
2+
3+
pragma solidity >=0.7.0 <0.9.0;
4+
5+
/**
6+
* Working with loops
7+
*/
8+
9+
contract LoopsInSolidity {
10+
// a list of numbers ranging from 1 to 10
11+
uint256[] public numbersList = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10];
12+
13+
function countMultiples(uint256 _number) public view returns (uint256) {
14+
// three part statement for a for loop
15+
// 1. Initialize start of loop
16+
// 2. Determine the length of the run time
17+
// 3. iterate or update the index after each turn
18+
// depending on the case - in our case we want to
19+
// increment the index after each run.
20+
21+
uint256 count = 0;
22+
for (uint256 index = 0; index < numbersList.length; index++) {
23+
// logic for the loop
24+
if (checkIfMultiple(numbersList[index], _number)) {
25+
count++;
26+
} else {
27+
continue;
28+
}
29+
}
30+
31+
return count;
32+
}
33+
34+
function checkIfMultiple(uint256 _num, uint256 _num2)
35+
public
36+
pure
37+
returns (bool)
38+
{
39+
// we will make use of modulo operator
40+
if (_num % _num2 == 0) {
41+
return true;
42+
} else {
43+
return false;
44+
}
45+
}
46+
}

0 commit comments

Comments
 (0)