codedamn

Whats wrong with my code? If I need to find Even,odd and check if its negative or not

Asked by Shariful Islam about 4 months ago

1
1contract ifElse{ 2 3 function checkpositiveNegative(int256 _num) public pure returns(string memory) { 4 string memory str; 5 if(_num<=0){ 6 str = 'Please enter positive number'; 7 } 8 else if(_num%2==0){ 9 str = 'This is a even Number'; 10 } 11 12 else{ 13 str = 'This is Odd number'; 14 } 15 return str; 16 } 17}

1 Answer

    1

    Hello Shariful, I couldn't find anything logically wrong with the code you've given, however, I see that the letter O is capital. The test cases might be absolutely checking the string passed.

    Here is an optimized revision on the code:

    
    contract ifElse {
    
        function checkpositiveNegative(int256 _num) public pure returns (string memory) {
            require(_num > 0, "Please enter a positive number");
    
            if (_num % 2 == 0) {
                return "This is an even number";
            } else {
                return "This is an odd number";
            }
        }
    }
    
    @codedamn

    Codedamn

    @codedamn

Your answer