So I had visited a lot of websites and still havent found anything on this topic. I want to write a function (in javascript) that checks the output of this code:
function mineHash(length) {
var result = '';
var characters = '01';
var charactersLength = characters.length;
for ( var i = 0; i < length; i++ ) {
result += characters.charAt(Math.floor(Math.random() * charactersLength));
}
return result;
}
console.log(mineHash(256));
and if the output has 70 or more 0s at the start it logs: ("Valid hash found!")
We create a string of 70 zeroes and check if the mineHash
result starts with these 70 zeroes. I also output the hash so we can check the result.
function mineHash(length) {
var result = '';
var characters = '01';
var charactersLength = characters.length;
for ( var i = 0; i < length; i++ ) {
result += characters.charAt(Math.floor(Math.random() * charactersLength));
}
return result;
}
const manyZeroes = '0'.repeat(70);
const hash = mineHash(256);
console.log(hash)
console.log(hash.startsWith(manyZeroes) ? 'Valid hash found!' : 'NOT a valid hash!')
// or an alternative suggested by Peter B:
console.log(/^0{70}/.test(hash) ? 'Valid hash found!' : 'NOT a valid hash!')