趁著記憶還在的時間,我把排列組合產生器改成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.

php.js中的array_count_value()的相關作者Kevin已經於2008年8月27日修復上述的BUG,重新再下載php.js(v1.37)以上就可以了^_^,你可以從下列連結確認資料來源,在最下面的回應中有提到修復的訊息
【2008-09-04補充】

The bug had fixed by Kevin on 27 August 2008.Try to download the latest "php.js" that version >= 1.37 again.info link
【updated on 2008-09-04】
arrow
arrow
    全站熱搜

    低溫烘培 發表在 痞客邦 留言(0) 人氣()