我有一个遍历一大串字符的循环。检查每个数字与另一个字符串中的单个数字并突出显示匹配...
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 Gupta” - 我已经删除了switcher
变量,但现在我得到了每个字符的多个实例:http: //jsfiddle.net/neuroflux/J4wbk/22/