0
// SPDX-License-Identifier: MIT

pragma solidity >=0.7.0 <0.9.0;

/**
 * @title FourthContract
 * @dev Exploring conditionals and loops
 */
contract FourthContract {

    uint[] public numbers = [10,20,30,40,50,60,70,80,90,100];

    function countOddNumbers() public view returns (uint) {
        uint count = 0;

        for(uint i = 0; i < numbers.length; i++) {
            ***if (isOddNumber(numbers[i])) {
                count = count + numbers[i];
            }***
        }
        return count;
    }

    function isOddNumber (uint _number) public view returns(bool) {
        if (_number % 2 == 1) { 
           return true;
        }
        else {
           return false;
        }
    }
}
0

your code is correct

here are few tweaks. I've added an odd number to the array because all are even and thus countOddNumbers returns 0.

// SPDX-License-Identifier: MIT

pragma solidity >=0.7.0 <0.9.0;

/**
 * @title FourthContract
 * @dev Exploring conditionals and loops
 */
contract FourthContract {

    uint[] public numbers = [10,20,30,40,50,60,70,80,90,100,11];

    function countOddNumbers() public view returns (uint) {
        uint count = 0;

        for(uint i = 0; i < numbers.length; i++) {
           if (isOddNumber(numbers[i])) {
                count++;
           }
        }
        return count;
    }

    function isOddNumber (uint _number) public pure returns(bool) {
        if (_number % 2 == 1) { 
           return true;
        }
        else {
           return false;
        }
    }
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.