[Solved] Why does “t” in “do{j++; t+=j;}while(j


Let’s clean it up:

var j = 0;
var t = 0;

do {
  j++;
  t += j;
} while (j < 5);

alert(t);

If you manually expand this (and I will), this is the equivalent code without loops:

var j = 0;
var t = 0;

j++; // j = 1
t += j;

j++; // j = 2
t += j;

j++; // j = 3
t += j;

j++; // j = 4
t += j;

j++; // j = 5
t += j;

alert(t);

Condensing this:

var t = 0;

t += 1;
t += 2;
t += 3;
t += 4;
t += 5;

alert(t);

Which results in:

var t = 1 + 2 + 3 + 4 + 5;

alert(t);

And I think you can do simple arithmetic:

var t = 15;

alert(t);

solved Why does “t” in “do{j++; t+=j;}while(j<5);" return 15? [closed]