I am trying to output this on console but it is not working as I want.
what I want is:
1 2 3 4 5
1 2 3 4 5
1 2 3 4 5 <---I WANT IT LIKE THIS
1 2 3 4 5
1 2 3 4 5
And not:
1
2
3
4
5
1
2 <-- I DONT WANT IT LIKE THIS
3
4
5
1
2
3
4
5
Please is there a way to achieve it purely on javascript without HTML with the code below?
let n = 5;
for (i = 1; i <= n; i++){
for (j = 1; j <= n; j++){
console.log(j + " ");
if (j = 5) {
console.log("\n");
} else continue;
}
}
I am trying to output this on console but it is not working as I want.
what I want is:
1 2 3 4 5
1 2 3 4 5
1 2 3 4 5 <---I WANT IT LIKE THIS
1 2 3 4 5
1 2 3 4 5
And not:
1
2
3
4
5
1
2 <-- I DONT WANT IT LIKE THIS
3
4
5
1
2
3
4
5
Please is there a way to achieve it purely on javascript without HTML with the code below?
let n = 5;
for (i = 1; i <= n; i++){
for (j = 1; j <= n; j++){
console.log(j + " ");
if (j = 5) {
console.log("\n");
} else continue;
}
}
Share
Improve this question
edited Mar 7, 2020 at 22:52
Nina Scholz
387k26 gold badges364 silver badges414 bronze badges
asked Mar 7, 2020 at 22:00
PaschalPaschal
191 silver badge9 bronze badges
7
-
1
Side note,
if (j = 5)
is invalid. It should beif (j == 5)
orif (j === 5)
– j08691 Commented Mar 7, 2020 at 22:04 -
use
<br/>
tag – Pranav C Balan Commented Mar 7, 2020 at 22:04 - @j08691 no it is not. This is perfectly valid code. It's just highly discouraged because of the possible misunderstanding. Did you really mean to assign a value or did you mean to write a parison? Like in this sample. – Thomas Commented Mar 7, 2020 at 22:07
-
@Thomas Why would he be assigning a value inside an
if
? Of course it's a parison and should be==
or===
. The OP is testing when to insert a break. – j08691 Commented Mar 7, 2020 at 22:09 - 1 @Thomas Ok technically it's a logic error and not a syntax error. I actually didn't say it was a syntax error in my ment. – j08691 Commented Mar 7, 2020 at 22:11
5 Answers
Reset to default 2Use a temporary string to add your i values and log it in the end of each outer loop
let n = 5;
for (i = 1; i <= n; i++){
var temp = "";
for (j = 1; j <= n; j++){
temp=temp+" "+j;
}
console.log(temp);
}
You could create an array with Array.from
and spread the values of it.
BTW, please declare all variables, because if not, these variables are global and could lead to crazy results if taken in some loops in different functions.
let length = 5;
for (var i = 1; i <= length; i++) {
console.log(...Array.from({ length }, (_, i) => i + 1));
}
let n = 5;
let line = "";
for (i = 1; i <= n; i++) {
line = "";
for (j = 1; j <= n; j++) {
line += " " + j;
}
console.log(line);
}
upgrading Sunil Lama's answer:
var n = 5;
var finaloutput = "";
for (i = 1; i <= n; i++){
var temp = "";
for (j = 1; j <= n; j++){
temp=temp+" "+j;
}
finaloutput += temp + "\r\n";
}
console.log(finaloutput);
Result in console:
1 2 3 4 5
1 2 3 4 5
1 2 3 4 5
1 2 3 4 5
1 2 3 4 5
Just wanted to point out something that other answers don't mention: new line characters.
You can form your string beforehand using new lines where necessary, then console.log()
once.
let string = ''
let columns = 5
let rows = 5
for (let i = 0; i < rows; i++) {
for (let j = 1; j <= columns; j++) {
string += j + ' '
}
string += '\n'
}
console.log(string)
发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745036531a4607552.html
评论列表(0条)