Javascript 如何在一行代码中转换此区块链脚本?
我知道如何在服务器上使用此区块链脚本,但我不知道如何将其转换为单个函数只有一次返回,最好是一行:Javascript 如何在一行代码中转换此区块链脚本?,javascript,algorithm,minify,blockchain,Javascript,Algorithm,Minify,Blockchain,我知道如何在服务器上使用此区块链脚本,但我不知道如何将其转换为单个函数只有一次返回,最好是一行: String.prototype.hashCode = function(){ if(this.length ==0){ return 0; }else{ return parseInt(this.split('').map(function(char){ return char.charCodeAt(0); }
String.prototype.hashCode = function(){
if(this.length ==0){
return 0;
}else{
return parseInt(this.split('').map(function(char){
return char.charCodeAt(0);
}).reduce(function(current, previous){
return previous + current;
}))+ (this.substr(1, this.length)).hashCode();
}
};
nonce = 0;
while(true){
hash = (transaction + nonce).hashCode() % 1234;
if (hash ==0){
break;
}
nonce++;
}
return nonce;
当然,事务是一个字符串。
我试图用hash替换它,但由于hashcode函数本身调用,我不知道如何在没有函数的情况下管理这个循环
您的结果必须在上面的minify链接中起作用:这是原始代码的直接翻译
function findNonce( data ){
var buffer, i, hash, nonce = -1;
data = '' + ( data || '' );
do {
hash = 0;
nonce++;
buffer = data + nonce;
i = buffer.length;
while(i--) hash += buffer.charCodeAt(i) * (i+1);
} while ( hash % 1234 );
return nonce;
};
一旦缩小规模
function findNonce(n){var e,o,r,t=-1;n=""+(n||"");do for(r=0,t++,e=n+t,o=e.length;o--;)r+=e.charCodeAt(o)*(o+1);while(r%1234);return t}
但是,如果需要更好的性能,应该对此进行优化,首先计算输入字符串的哈希值,然后只对每个nonce值进行计算
function findNonceFast( data ){
var preHash, noncePos, nonce = -1, code;
function hashCalc( s, hash, pos ){
var i = s.length; while(i--) hash += s.charCodeAt(i) * (i+pos+1);
return hash;
};
data = '' + ( data || '' );
preHash = hashCalc( data, 0, 0 );
noncePos = data.length;
do {
nonce++;
code = hashCalc( ''+nonce, preHash, noncePos );
} while (code % 1234)
return nonce;
};
缩小
function findNonceFast(n){function r(n,r,t){for(var e=n.length;e--;)r+=n.charCodeAt(e)*(e+t+1);return r}var t,e,o,a=-1;n=""+(n||""),t=r(n,0,0),e=n.length;do a++,o=r(""+a,t,e);while(o%1234);return a}
快速回复,优化和提示,以建立其他功能。你是一个真正有用的JS大师。我很愿意和你这样的人一起工作。选择作为答复。