JavaScript Console Output to Single Line with For and While Loop -
i need figure out how output list of numbers in loop console. however, output console must in same line, opposed cascading down page every new number. below, have 2 separate blocks of code accomplish same thing. 1 uses while loop , other uses loop. if there's difference between being able accomplish in loop versus while loop, i'd know well. thank you.
while loop
var number = 2; while (number <=10) { console.log(number++); number = number + 1; }
for loop
for (var number = 2; number <= 10; number = number + 2) console.log(number);
while:
var number = 2, output = []; while (number <=10) { output.push(number++); number = number + 1; } console.log.apply(console, output);
for:
var number, output=[]; (number = 2; number <= 10; number = number + 2) { output.push(number); } console.log.apply(console, output);
note, recommend don't for(var x...
- gives false sense of scope var x - that's opinion on part
Comments
Post a Comment