Building the getBlock method

Let's build a new method called getBlock that will take the given blockHash and search the entire blockchain for the block associated with that particular hash. In order to build the getBlock method, follow these steps: 

  1. Go to the dev/blockchain.js file and after the chainIsValid method, define this new method as follows: 
Blockchain.prototype.getBlock = function(blockHash) { };
  1. Inside this method, we want to iterate through the entire blockchain and search for the block that has a particular blockHash value. Then, this method will return that specific block to us. We're going to do all this with the help of a for loop:
Blockchain.prototype.getBlock = function(blockHash) {     this.chain.forEach(block => ...

Get Learn Blockchain Programming with JavaScript now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.