My output is
1
1
2
1
2
3
…
The output I am looking for is
1 1 2 1 2 3 1 2 3 4 1 2 3 4 5
var x,y;
for(x=1; x <= 5; x++){
for (y=1; y <= x; y++) {
console.log(y)
}
}
You could take a single loop with a part variable and one for the full string.
Then you need to add a space only if the string is not empty and add in each loop the new value and the actual part to the full string.
var i,
part = '',
full = '';
for (i = 1; i <= 5; i++) {
part += (part && ' ') + i;
full += (full && ' ') + part;
}
console.log(full);
You are console logging each time which puts it on a new line. It's a better idea to store numbers in an array and then print out one by one.
var x, y, myArray[];
for (x = 1; x <= 5; x++) {
for (y = 1; y <= x; y++) {
myString += y.toString() + " ";
}
}
console.log(myString);
You could also place numbers in an array and output one by one.
var str = "", then inside loop just do:str += valueYouWantToConcatenate. Then log it after the loops