I have a loop that goes through a huge string of characters. Checks each digit against individual digits in another string and highlights the matches…
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 Example: http://jsfiddle.net/neuroflux/J4wbk/12/
My question is, how come it’s only ever highlighting the 7's? I’ve been scratching my head for ages over this!
[EDIT]
Thanks to “@Yograj Gupta” – I’ve removed the switcher variable, but now I get multiple instances of each character: http://jsfiddle.net/neuroflux/J4wbk/22/
Well, you’re definitely doing this the hard way. Use
indexOfinstead (or, as Johan pointed out,jQuery.inArray):http://jsfiddle.net/CrossEye/euGLn/1/
Although there’s lots of other clean-up to recommend here, at least the loop is easier.
— Scott