Privacy preferences

Choose separately whether we may use analytics and advertising measurement. Both stay off unless you turn them on. You can change these choices any time.

All challenges

Challenge 9

tx.origin Misuse

LowArrays and loops

Challenge

Add numbers to an array and calculate their sum. Review the loop in the published sample.

Starter code

// SPDX-License-Identifier: MIT
pragma solidity ^0.8.0;

contract ArraySum {
  uint[] public numbers;

  function addNumber(uint _number) public {
    numbers.push(_number);
  }

  function calculateSum() public view returns (uint) {
    uint sum = 0;
    // Complete the loop shown in the source challenge.
    return sum;
  }
}
Show hint

Iterate over each element and add it to sum.