Ask coding questions

← Back to all posts
[JS] Is there anyway to make this code smaller but have the same function?
MisledWater79 (0)
try {
  txt += ' --'+data_users[0]+': '+data_num_money[0]+'-- \n';
  try {
    txt += ' --'+data_users[1]+': '+data_num_money[1]+'-- \n';
    try {
      txt += ' --'+data_users[2]+': '+data_num_money[2]+'-- \n';
      try {
        txt += ' --'+data_users[3]+': '+data_num_money[3]+'-- \n';
        try {
          txt += ' --'+data_users[4]+': '+data_num_money[4]+'-- \n';
        } catch (e){};
      } catch (e){};
    } catch (e){};
  } catch (e){};
} catch (e){};
Comments
hotnewtop
AlexDaCoder (20)
if (data_users[4] !== undefined) const max = 4;
else if (data_users[3] !== undefined) const max = 3;
else if (data_users[2] !== undefined) const max = 2;
else if (data_users[1] !== undefined) const max = 1;
else if (data_users[0] !== undefined) const max = 0;
else const max = -1;
for (let i = 0; i <= max; i++) {
  txt += ' --' + data_users[i] + ": " + //etc.
}