I'm working on a project where a function takes an array of numbers and converts them into blocks on the screen. It's working fine, but in that function there are four if statements all checking if 5 values are true:
if (adj[1] && adj[2] && n > 13 && n % 14 && blocks[n - 15] !== 1) {
cor.push([0, 0]);
}
if (adj[0] && adj[2] && n > 13 && (n + 1) % 14 && blocks[n - 13] !== 1) {
cor.push([1, unit * 14 / 16]);
}
if (adj[0] && adj[3] && n < 98 && (n + 1) % 14 && blocks[n + 15] !== 1) {
cor.push([3, unit * 14 / 16]);
}
if (adj[1] && adj[3] && n < 98 && (n % 14) && blocks[n + 13] !== 1) {
cor.push([2, 0]);
}
That's the actual code, and as you can see many of those values are repeated. The point is, I want my code to look cleaner and smoother, and I feel like there is a way to simplify or shorten this section.
Let me know if you'd like to see the rest of the code, but that shouldn't matter, just consider this:
if (a && c && e && g && !i) variable = m + o;
if (a && d && e && h && !j) variable = m + p;
if (b && d && f && g && !k) variable = n + q;
if (b && c && f && h && !l) variable = n + r;
I'm just wondering if there is a way to simplify the above code; maybe using chained ternary expressions, switch statements, or an array of values to choose from, but hopefully be more readable and easier to modify.
Thanks for helping out!
Note: I'm not super concerned with performance, just simplicity and readability.
Aucun commentaire:
Enregistrer un commentaire