0

I am stuck at Step-6 of Bitdegree's Solidity tutorial. Question is to Emit an event ScoreChanged with two arguments: int amount (equal to the value that was sent) and bool direction (true for up function and false for down).

This is my solution for this,I have previously checked it with remix and there are no errors in my code. I am able to pass second test case where I have to write bool direction event but stuck at int amount event.I am not able to figure out why my solution is not getting accepted? I hope someone can help me. I am beginner in Solidity.

pragma solidity 0.4.17;
contract TugOfWar {

    int public score = 0;
    int constant endAt = 1 ether;
    bool public gameOver = false;
    bool public _direction;
    /*I even tried this
    event ScoreChanged(int indexed _amount, bool indexed _direction);*/
    event ScoreChanged(int _amount, bool _direction);
    event GameEnded(bool _gameOver);

    function up() external payable {
        require(msg.value > 0);
        require(!gameOver);

        int value = int(msg.value);
        score += value;

         ScoreChanged(value,true);

        checkIfGameOver();
    }

    function down() external payable {
        require(msg.value > 0);
        require(!gameOver);

        int value = int(msg.value);
        score -= value;

         ScoreChanged(value,false);

        checkIfGameOver();
    }

    function checkIfGameOver() internal {
        if(score >= endAt || score <= endAt * -1) {
            gameOver = true;
             GameEnded(gameOver);
        }
    }
}
5
  • What's the problem / what's your question?
    – user19510
    Commented Mar 27, 2018 at 2:45
  • I am not able to figure out why my solution is not getting accepted? Commented Mar 27, 2018 at 2:48
  • Is there a test case you have to pass? Do you know what that test is doing? Your code definitely emits an event.
    – user19510
    Commented Mar 27, 2018 at 2:50
  • Perhaps if you shared the text of what you're supposed to do, somebody might be able to spot the issue.
    – user19510
    Commented Mar 27, 2018 at 2:51
  • No there is no as such test case which I need to pass. These is the exact text what is written over there: 'Emit an event ScoreChanged with two arguments: int amount (equal to the value that was sent) and bool direction (true for up function and false for down)' Commented Mar 27, 2018 at 2:57

3 Answers 3

1

I just zipped through to lesson six. The second task is:

Emit an event ScoreChanged with two arguments: int amount (equal to the value that was sent) and bool direction (true for up function and false for down)

Your parameters are incorrectly named. Rename them to match:

event ScoreChanged(int amount, bool direction);
12
  • I am able to pass the second task with my code. The problem lies in the 1st task. I hope you can help me out. Commented Mar 27, 2018 at 3:01
  • Your existing code looks correct for the first task.
    – user19510
    Commented Mar 27, 2018 at 3:02
  • Oh, sorry, your parameters are incorrectly named.
    – user19510
    Commented Mar 27, 2018 at 3:02
  • Yep, there is no error but I am not able to pass it and according to their policy, I can't proceed to next chapter until I have successfully submitted previous programming exercises. Commented Mar 27, 2018 at 3:03
  • OMG !! Changing parameters' name helped but one can name them anything, correct? Commented Mar 27, 2018 at 3:06
0

here is the correct code.

pragma solidity ^0.4.17;

contract TugOfWar {

    int public score = 0;
    int constant endAt = 1 ether;
    bool public gameOver = false;
    event GameEnded(bool gameOver);
    event ScoreChanged(int amount,bool direction);

    function up() external payable {
        require(msg.value > 0);
        require(!gameOver);

        int value = int(msg.value);
        int amount= value;
        bool direction=true;
        score += value;

        //
        emit ScoreChanged(amount,direction);

        checkIfGameOver();
    }

    function down() external payable {
        require(msg.value > 0);
        require(!gameOver);

        int value = int(msg.value);
        int amount=value;
        bool direction=false;
        score -= value;

        //
        emit ScoreChanged(amount,direction);

        checkIfGameOver();
    }

    function checkIfGameOver() internal {
        if(score >= endAt || score <= endAt * -1) {
            gameOver = true;

            //
            emit GameEnded(gameOver);
        }
    }
}
-1

pragma solidity ^0.4.17;

contract TugOfWar {

int public score = 0;
int constant endAt = 1 ether;
bool public gameOver = false;
**event ScoreChanged (int amount, bool direction);
event GameEnded(bool gameOver);**

function up() external payable {
    require(msg.value > 0);
    require(!gameOver);

    int value = int(msg.value);
    score += value;
    **emit ScoreChanged(value, true);**

    //

    checkIfGameOver();
}

function down() external payable {
    require(msg.value > 0);
    require(!gameOver);

    int value = int(msg.value);
    score -= value;
    **emit ScoreChanged(value, false);**

    //

    checkIfGameOver();
}

function checkIfGameOver() internal {
    if(score >= endAt || score <= endAt * -1) {
        gameOver = true;
      
       **emit GameEnded(gameOver);**
        //
    }
}

}

1
  • Can you add an explanation how this solves the question?
    – Ismael
    Commented Sep 16, 2021 at 4:45

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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