Is there a more efficient way to find two adjacent elements with values of 1:2 or 1:3 in an array without counting the third element? The current method works but appears messy. Any suggestions on optimization, possibly by skipping the next iteration during each loop?
const history = ["1:2", "1:11", "1:9", "1:6", "1:9", "1:2", "1:3", "1:10", "1:9", "1:3", "1:3"];
function getStats(history) {
let trigger = 0;
let dfault1 = 0;
$.each(history, function(index, value) {
if (((history[index] == '1:2') || (history[index] == '1:3')) && ((history[index+1] == '1:2') || (history[index+1] == '1:3'))) {
if (trigger === 0) {
dfault1++;
trigger = 1;
} else {
trigger = 0;
}
} else {
trigger = 0;
}
});
console.log(dfault1);
}
dfault1 = 2 because:
When history[5] equals '1:2' and history[6] equals '1:3', dfault++ (1)
then
When history[9] equals '1:3' and history[10] equals '1:3', dfault++ (2)
p.s. Apologies for any language issues. Feel free to ask for clarification if needed.