Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

We want an event to let our front - end know every time a new zombie was created, so the app can display it .

We want an event to let our front-end know every time a new zombie was created, so the app can display it.
1.Declare an event called NewZombie. It should pass zombieId (a uint), name (a string), and dna (a uint).
2.Modify the _createZombie function to fire the NewZombie event after adding the new Zombie to our zombies array.
3.You're going to need the zombie's id. array.push() returns a uint of the new length of the array - and since the first item in an array has index 0, array.push()-1 will be the index of the zombie we just added. Store the result of zombies.push()-1 in a uint called id, so you can use this in the NewZombie event in the next line.
pragma solidity >=0.5.0<0.6.0;
contract ZombieFactory {
// declare our event here
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));
// and fire it here
}
function _generateRandomDna(string memory _str) private view returns (uint){
uint rand = uint(keccak256(abi.encodePacked(_str)));
return rand % dnaModulus;
}
function createRandomZombie(string memory _name) public {
uint randDna =_generateRandomDna(_name);
_createZombie(_name, randDna);
}
}

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access with AI-Powered Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Students also viewed these Databases questions