我有一个循环,通过一个巨大的字符串。针对检查中另一个字符串个别数字每个数字,并强调比赛...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/
BOOM! - 谢谢@Scott!我会尽快接受! –
呸,1分钟比我快-.- – Johan
您可以替换为用'的(在这些正是DeCypher变种X)'和条件如果与'如果(〜systemPass.indexOf(正是DeCypher [X]))'进一步降低的声明尽管我们都使用'indexOf'在我们的答案代码:) http://jsfiddle.net/jax2u/ –