Operation on State Variables

At the end of the previous exercises, you're contract should look like the following

pragma solidity ^0.4.24;

contract SpacecryptFactory {
  // Default spaceship timeout
  uint public timeout = 60;

  // Best factory name ever made
  bytes32 public name = 'SuperCryptor 2000';

If I tell you that the timeout should not be in minutes but in seconds, what would you do? You would grab the nearest calculator, type 60*60 and replace timeout`s value by 3600 (the result 😉).

Arithmetic operation


Solidity has thought about this and allows you to enter the operation directly

uint timeout = 60 * 60;

It's more readable and allows us to even use this operation with variables

uint timeoutInMinutes = 60;
uint timeoutInSeconds = timeoutInMinutes * 60;
Amiral tip

Notice that timeoutInSeconds is declare after timeoutInMinutes because it uses its value

List of operations

The world would be a harsh place if multiplication was the only operation allowed. So here is the list of all common operations natively supported by Solidity

Operation Example
Addition 2 + 1 = 3
Subtraction 2 - 1 = 1
Multiplication 2 * 3 = 6
Division 3 / 2 = 1
Modulus 3 % 2 = 1

It is wise to notice here that dividing by 0 or having a negative number for an uint could have unexpected effects.



In the code, I have added a new variable called halfway that should be equal to half of timeout. Initialise it
pragma solidity ^0.4.24; contract SpacecryptFactory { // Default spaceship timeout in Minutes uint public timeout = 60; uint public halfway = ; // Name of your factory bytes32 public name = 'SuperCryptor 2000'; }

When everything is complete, let's head to the next step. There is an old friend waiting for us.

results matching ""

    No results matching ""