【CryptoZombies】lesson1 Chapter 12: Putting It Together

test

1.Create a public function named createRandomZombie. It will take one parameter named _name (a string with the data location set to memory). (Note: Declare this function public just as you declared previous functions private)

2.The first line of the function should run the _generateRandomDna function on _name, and store it in a uint named randDna.

3.The second line should run the _createZombie function and pass it _name and randDna.

4.The solution should be 4 lines of code (including the closing } of the function).

pragma solidity  >=0.5.0 <0.6.0;

contract ZombieFactory {

   uint dnaDigits = 16;
   uint dnaModulus = 10 ** dnaDigits;

   struct Zombie {
       string name;
       uint dna;
   }

   Zombie[] public zombies;

   function _createZombie(string memory _name, uint _dna) private {
       zombies.push(Zombie(_name, _dna));
   } 

   function _generateRandomDna(string memory _str) private view returns (uint) {
       uint rand = uint(keccak256(abi.encodePacked(_str)));
       return rand % dnaModulus;
   }

   // start here
   function createRandomZombie(string memory _name) public {
       uint randDna = _generateRandomDna(_name);
       _createZombie(_name, randDna);
   }

}

Putting It Together:統合


この記事が気に入ったらサポートをしてみませんか?