close
趁著記憶還在的時間,我把排列組合產生器改成Javascript版本了,改版過程多虧了有php.js這個函式庫讓我減少了大部分必需修改的,不過有些Javascript比較特殊的地方,還真花了我不少時間,像是函式的變數初始值,我參考了網路上的資源,有下列三種做法
//第1種(bad),if vairable=null,variable=false variable = variable || defaultValue; //第2種(bad),if vairable=null,variable=false variable ? variable : variable = defaultValue; //第3種(good),if vairable=null,variable=defaultValue if(variable == null) { variable = defaultValue;}
試用的結果,第1和2種有一種小漏洞,如果傳入的值是false,那麼系統以為你沒有傳值,所以你所傳入的將不是false,而是變成預設值第3種就沒有這種問題了,這點使用上要稍注意一下
再來就是二維列陣,Javascript沒辦法像PHP那樣的寫法
//錯誤的方法(error) var a = new Array(); var b = new Array(1,2,3); a[0] = b;
上面的寫法是錯的,應該改成下面的寫法
//正確的方法(correct) var a = new Array(); var b = new Array(1,2,3); a[0] = new Array(); for(var i=0; i<b.length; i++){ a[0][i] = b[i]; }
好了,廢話不再說了,Javascript版程式碼如下:
注意這個程式碼有使用到php.js的函式庫,你必需先載入此檔後才能正常運作 下載網址在程式碼的註解中有提到
/* ************************************************************** * Web: http://doublekai.org/blog/ * Licenses: MIT and GPL * source: http://www.php.happycodings.com/Algorithms/code21.html * modifier: sankai * date:2008-08-25 * NOTE: It's must include the php.js library * http://kevin.vanzonneveld.net/code/php_equivalents/php.js * **************************************************************/ function permutations(letters,num,repeatPick){ if(repeatPick == null){ repeatPick = true; } var result = new Array(); if(is_string(letters)){ var letters = letters.split(''); var returnType = 'string'; } else { var returnType = 'array'; } if(is_array(letters)){ var last = array_fill(0,num-1,letters[0]); while(count(array_diff(last,array_fill(0,num,letters[count(letters)-1]))) != 0){ if(repeatPick){ if(returnType == 'string'){ array_push(result,join('',last)); } else { var key = result.length++; result[key] = new Array(); for(var i=0; i<last.length; i++){ result[key][i] = last[i]; } } } else{ if(count(array_count_values(last)) == num){ if(returnType == 'string'){ array_push(result,join('',last)); } else { var key = result.length++; result[key] = new Array(); for(var i=0; i<last.length; i++){ result[key][i] = last[i]; } } } } last = char_add(letters,last,(num-1)); } if(repeatPick){ if(returnType == 'string'){ array_push(result,join('',last)); } else { var key = result.length++; result[key] = new Array(); for(var i=0; i<last.length; i++){ result[key][i] = last[i]; } } } else{ if(count(array_count_values(last)) == num){ if(returnType == 'string'){ array_push(result,join('',last)); } else { var key = result.length++; result[key] = new Array(); array_push(result[key],last); } } } return result; } else { return new Array(); } } function char_add(letters,last,index){ if(last[index] != letters[count(letters)-1]){ last[index] = letters[eval(array_search(last[index],letters))+1]; return last; }else{ last = changeall(last,letters[0],index); return char_add(letters,last,index-1); } } function changeall(array,element,start,end){ if(start == null){ start = 0; } end = end || count(array)-1; for(var i=start; i<=end; i++){ array[i] = element; } return array; }
目前這個函式在IE會出現問題,主要原因是因為IE不支援array.forEach的方式,因為php.js中的array_count_values()會使用到這個,這算是array_count_values()的bug,我已經向原作者提出回報,當作者完成修復後,我會再補上修補的方法。
the function can't run in IE broswer,because IE can't support the method "array.forEach" that be used for array_count_value() in php.js library. I alredy report the bug to author.I will post the patch detail,if the bug be fixed.
the function can't run in IE broswer,because IE can't support the method "array.forEach" that be used for array_count_value() in php.js library. I alredy report the bug to author.I will post the patch detail,if the bug be fixed.
全站熱搜