Remove gross for loop

This commit is contained in:
Jack Bond-Preston 2018-12-05 20:32:56 +00:00
parent 0353778bfb
commit bf04058082

View File

@ -7,13 +7,14 @@ function swapCase(c) {
} }
function collapsePolymer(polymer) { function collapsePolymer(polymer) {
for (let i = 0; i < polymer.length; i++) { let i = 0;
let c = polymer[i]; while(i < polymer.length - 1) {
if (polymer[i] === swapCase(polymer[i + 1])) {
if (i !== polymer.length - 1 && c === swapCase(polymer[i + 1])) {
polymer.splice(i, 2); polymer.splice(i, 2);
i = Math.max(-1, i - 2); i = Math.max(-1, i - 2);
} }
i++;
} }
return polymer.length; return polymer.length;