How to reduce } (javascript)


#1

Alright I’m just curious when I’m coding and using } } } } all the time at the end, is there a way I can summarize all the } ?

ie) What’s highlighted bold

while(true) {
var enemy = hero.findNearestEnemy();
if(enemy) {
// Find the distance to the enemy with distanceTo.
var dte = hero.distanceTo(enemy);
// If the distance is less than 5 meters…
if (dte < 5) {
// … if “cleave” is ready, cleave!
if (hero.isReady(“cleave”)) {
hero.cleave(enemy);

        // ... else, just attack.
} else {
    if(enemy) {
    hero.attack(enemy);
**}**

} <<All
} <<Of
} <<This
} <<?


#2

No, I don’t think so.


#3

Each identifier closes a loop/etc., so no. It organizes everything better that way though; otherwise it gets confusing and inconvenient.


#4

Oh ok, thank you! I was just wondering if I could do like }*5 or something of the sort to end all or minimize it