Array

Counting the number of fans of the SpaceMuffin is great, but what if we want to check who likes it (everyone obviously, but some people might forget to contact us)?

It's where arrays come to play.

What is it

An array is a variable that groups other variables of the same type. In the case of the SpaceMuffin, you want to represent a group of address in a fans variable. So all fans will be within a unique variable.

Declaration

You declare an array of uint as follow

uint[] public integers;

Amiral tip Exactly as you would declare a regular contract variable

Exercise
Correct!
False!

Loading...

Declare fans as an array
pragma solidity ^0.4.24; contract SpaceMuffin { uint public numberOfFans = 0; // declare fans public here // I actually need it to check if your code is correct function like() public { numberOfFans = numberOfFans + 1; // it should be updated in the next exercise } }

Hint

fans should be a public variable

Members

You might know think that you've declare a variable but it's not useful.

Array type has two attributes that are described below: push and length

Push

To add an element, you just have to use the push member of Array.

uint[] public integers;

function add() public {
  integers.push(0);
}

This will insert 0, which is a uint variable, in integers. If you call add once, integers will be equal to [0], if twice [0, 0], etc...

Exercise
Correct!
False!

Loading...

Add the sender of the message when it likes the muffin
pragma solidity ^0.4.24; contract SpaceMuffin { uint public numberOfFans = 0; address[] public fans; // I actually need it to check if your code is correct function like() public { numberOfFans = numberOfFans + 1; // add the new fan here } }

Hint

The sender of the message is a special variable

Access

Now that you have the list of SpaceMuffin fans, you would like to access it. Elements in an array are indexed from 0 to the end of the array.

For example, the second element of [3, 4, 7, 2] has index 1 and value 4.

In Solidity, such an element would be written as follow

uint[] internal integers;

function secondElement() view public returns (uint) {
    return integers[1];
}

Obviously, you might want to access an index given by a variable. Instead of integers[1], you can put any expression returning a uint within [expression].

uint[] internal integers;
uint internal index = 0;

function element() view public returns (uint) {
    return integers[index];
}

Exercise
Correct!
False!

Loading...

Create access, a function returning the i-th fans (the first fan is the 0-th)
pragma solidity ^0.4.24; contract SpaceMuffin { uint public numberOfFans = 0; address[] public fans; function like() public { numberOfFans = numberOfFans + 1; fans.push(msg.sender); } // write access function here }

Hint

  • fans does not return uint
  • What is the state mutability of access function?

Length

To know how many elements are stored in the array, you just have to access length attribute

uint[] public integers;

function numberOfIntegers() view public returns (uint) {
    return integers.length;
}

Exercise
Correct!
False!

Loading...

Write a modifier checking if the index is less than number of fans
pragma solidity ^0.4.24; contract SpaceMuffin { address[] public fans; // write modifier `withinBounds` function like() public { // number of fans is equal to fans.length // you thought coding was only about copy/pasting? // numberOfFans = numberOfFans + 1; fans.push(msg.sender); } function access(uint index) view public withinBounds(index) returns (address) { return fans[index]; } }

Hint

  • The modifier require a condition on fans.length
  • You should write _; at the end of the modifier

results matching ""

    No results matching ""