2012-09-26 92 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。长度时间。