|
1 |
| -// SPDX-License-Identifier: MIT |
2 |
| -pragma solidity ^0.8.0; |
3 |
| - |
4 |
| -/** |
5 |
| - * @title Array Addition. |
6 |
| - * @author Anthony (fps) https://github.com/fps8k. |
7 |
| - * @dev Contract to demonstrate how an array is added. |
8 |
| - */ |
9 |
| - |
10 |
| -contract AddArray { |
11 |
| - /** |
12 |
| - * @dev Takes in an array of numbers and adds them and returns the cumultative total. |
13 |
| - * @param _arr array of numbers |
14 |
| - * @return total => sum of array. |
15 |
| - */ |
16 |
| - function addArr(uint256[] memory _arr) public pure returns (uint256 total) { |
17 |
| - /// @dev Initialize the total we need to return to 0. |
18 |
| - uint256 sum = 0; |
19 |
| - |
20 |
| - /// @dev Loop through the array. |
21 |
| - for (uint256 i = 0; i < _arr.length; i++) { |
22 |
| - /// @dev On every element, add the value of the element to the current sum. |
23 |
| - /// @dev For sums > the maximum uint value, solidity versions > 0.8.0 will revert on overflow. |
24 |
| - sum += _arr[i]; |
25 |
| - } |
26 |
| - |
27 |
| - /// @dev Return the total. |
28 |
| - total = sum; |
29 |
| - } |
30 |
| -} |
| 1 | +// SPDX-License-Identifier: MIT |
| 2 | +pragma solidity ^0.8.0; |
| 3 | + |
| 4 | +/** |
| 5 | + * @title Array Addition. |
| 6 | + * @author [Anthony (fps)](https://github.com/fps8k) |
| 7 | + * @dev Contract to demonstrate how an array is added. |
| 8 | + */ |
| 9 | + |
| 10 | +contract AddArray { |
| 11 | + /** |
| 12 | + * @dev Takes in an array of numbers and adds them and returns the cumultative total. |
| 13 | + * @param _arr array of numbers |
| 14 | + * @return total => sum of array. |
| 15 | + */ |
| 16 | + function addArr(uint256[] memory _arr) public pure returns (uint256 total) { |
| 17 | + /// @dev Initialize the total we need to return to 0. |
| 18 | + uint256 sum = 0; |
| 19 | + |
| 20 | + /// @dev Loop through the array. |
| 21 | + for (uint256 i = 0; i < _arr.length; i++) { |
| 22 | + /// @dev On every element, add the value of the element to the current sum. |
| 23 | + /// @dev For sums > the maximum uint value, solidity versions > 0.8.0 will revert on overflow. |
| 24 | + sum += _arr[i]; |
| 25 | + } |
| 26 | + |
| 27 | + /// @dev Return the total. |
| 28 | + total = sum; |
| 29 | + } |
| 30 | +} |
0 commit comments