2012-09-26 90 views
2

我有一個循環,通過一個巨大的字符串。針對檢查中另一個字符串個別數字每個數字,並強調比賽...Javascript循環內循環字符測試

var decypher = "782137829431783498892347847823784728934782389"; 

var systemPass = "789544"; 

for (var x = 0; x < decypher.length; x++) { //loop through the array 
    var switcher = 0; //not run this row yet 
    for (var p = 0; p < systemPass.length; p++) { //loop through each digit in the password 
     if(eval(decypher[x]) === eval(systemPass[p])) { //if the password digit matches the array digit 
      if (switcher === 0) { //not run yet... 
       $('body').append("<p style='color: green; float: left;'>"+decypher[x]+"</p>"); 
       switcher = 1; //finished running 
      } 
     } else { //no match 
      if (switcher === 0) { //not run yet... 
       $('body').append("<p style='color: silver; float: left;'>"+decypher[x]+"</p>"); 
       switcher = 1; //finished running 
      } 
     } 
    } 
} 

的jsfiddle例子:http://jsfiddle.net/neuroflux/J4wbk/12/

我的問題是,怎麼跟永遠只突出7's我一直在撓撓我的頭!

[編輯]
感謝「@Yograj古普塔」 - 我已經刪除了switcher變量,但現在我得到每個字符的多個實例:http://jsfiddle.net/neuroflux/J4wbk/22/

回答

6

那麼,你絕對是這麼做的。使用indexOf代替(或者,如約翰指出,jQuery.inArray):

http://jsfiddle.net/CrossEye/euGLn/1/

var decypher = "782137829431783498892347847823784728934782389"; 
var systemPass = "789544"; 

for (var x = 0; x < decypher.length; x++) { 
    // if(systemPass.indexOf(decypher[x]) > -1) { // Thanks, Johan 
    if ($.inArray(decypher[x], systemPass) > -1) { 
     $('body').append("<p style='color: green; float: left;'>"+decypher[x]+"</p>"); 
    } else { //no match 
     $('body').append("<p style='color: silver; float: left;'>"+decypher[x]+"</p>"); 
    } 
} 

雖然還有很多其他的清理在這裏建議,至少循環更容易。

- 斯科特

+0

BOOM! - 謝謝@Scott!我會盡快接受! –

+0

呸,1分鐘比我快-.- – Johan

+0

您可以替換爲用'的(在這些正是DeCypher變種X)'和條件如果與'如果(〜systemPass.indexOf(正是DeCypher [X]))'進一步降低的聲明儘管我們都使用'indexOf'在我們的答案代碼:) http://jsfiddle.net/jax2u/ –

0

我在你jsfiddle 我做出改變現在更新了我的代碼,它應該解決您的問題

var decypher = "782137829431783498892347847823784728934782389"; 

    var pass = 789544; 
    pass = "" + pass; var temp = ''; 
    for(var k =0; k < pass.length; k++){ 
     temp += pass[k] + '|'; 
    } 
    temp = temp.substring(0, temp.length-1) ; 

    console.log(new RegExp(temp,'g')); 

    document.body.innerHTML =decypher.replace(new RegExp(temp,'g'), function(a){ 

     return '<span>'+a + '</span>'; 

    }); 

​ 
+1

我不認爲他其實是想更換7的。 :) –

1

它顯示只有7的,因爲你是第一個迭代製作切換臺= 1的內部循環。

所以當它來到7你的systemPass var中的0索引上存在7時,所以首先重複檢查它,並將其顯示爲綠色。但對於所有它去其他和顯示在銀色和切換器成爲1。

所以,你應該檢查你的值對indexOf函數。

+0

啊*我看!*。但現在我得到http://jsfiddle.net/neuroflux/J4wbk/19/(每個角色的多個實例...) –

0

檢查此琴:http://jsfiddle.net/2HvwT/2/

注:已更新的indexOf()的小提琴由Scott Sauyet

代碼的建議:

for (var x = 0; x < decypher.length; x++) { //loop through the array 
    var found = systemPass.indexOf(decypher[x]); 
    $('body').append("<p style='color: "+(found !== -1?'green':'silver')+"; float: left;'>"+decypher[x]+"</p>"); 
} 
1

喜歡這個?或者我錯過了什麼?

var decypher = "782137829431783498892347847823784728934782389".split(''); 

var systemPass = "789544".split(''); 

$.each(decypher, function(i, v){ 

    if(systemPass.indexOf(v) !== -1) 
     $('body').append("<p style='color: green; float: left;'>"+ v +"</p>"); 
    else 
     $('body').append("<p style='color: silver; float: left;'>"+ v +"</p>"); 

}); 

http://jsfiddle.net/J4wbk/22/

+0

Upvoted慢1分鐘;) –

1

退房這個... http://jsfiddle.net/J4wbk/26/

我認爲這將有助於

說明: 要分配切換= 1內loop.So內,後首先與切換器匹配的是1,總是執行其他部分。 而且您正在獲取多個字母,因爲您將每個字母追加到內部循環中的decypher,並添加了systemPass。長度時間。