S
S
Sana_K2018-10-09 17:12:44
JavaScript
Sana_K, 2018-10-09 17:12:44

How to reverse the result of a loop?

There is a cycle:

var namber = 3;
var namberTwo = namber * namber;
for (var i = 1; i <= namber; i += 1) {
  process.stdout.write(i + "\t");
  for (var a = i + namber; a <= namberTwo; a += namber) {
    if (a <= (namberTwo - namber)) {
      process.stdout.write(a + "\t");
    } else {
      process.stdout.write(a + "\n");
    }
  }
}

Which outputs the result:
1       4       7
2       5       8
3       6       9

How to reverse the result? That is, output:
3       2       1
6       5       4
9       8       7

Answer the question

In order to leave comments, you need to log in

3 answer(s)
0
0xD34F, 2018-10-09
@Sana_K

var num = 3; 

for (var i = 1; i <= num; i++) {
  for (var j = 0; j < num; j++) { 
    process.stdout.write(`${i * num - j}\t`);
  }
  process.stdout.write('\n');
}

R
Randel Kovalsky, 2018-10-10
@Randel

Namber is cool...

I
it-pavel, 2018-10-09
@it-pavel

for (var i=number; i <= 1; i--)

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question