+ 1
I have number(N) and i have to print all the number between 1 to square of that number(N*N)
function patternOfN(N) { for(i=1;i<=N;i++){ let s = ""; for(j=i;j<=i*N;j++){ s = s+j+" "; } console.log(s); } } input is 4 desire output 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 but what i get 1 2 3 4 2 3 4 5 6 7 8 3 4 5 6 7 8 9 10 11 12 4 5 6 7 8 9 10 11 12 13 14 15 16 so where i am wrong ?
17 ответов
+ 1
count=0;
for(i=1;i<=N*N;i+=N){
count+=1;
let s = "";
for(j=i;j<=count*N;j++){
s = s+j+" ";
}
console.log(s);
}
here I solve this
+ 2
TINKLE DASH
Check step by step and read carefully
It will print
+ 1
Manav Roy but I have to put that as row and column
+ 1
TINKLE DASH
Use a 3rd variable
function patternOfN(N) {
var k = 0;
for(i = 1; i <= N; i++) {
let s = "";
for(j = 1; j <= N; j++) {
k++;
s = s + k + " ";
}
console.log(s);
}
}
patternOfN(4)
+ 1
TINKLE DASH You can just break lines on every output which is multiple of the input.
Or, if you prefer nesting, do both loops from 1 to N. Then output the sum of i * N and j. This way, every line gets incremented by N, which is what I guess you need (the description isn't clear).
+ 1
Ah, it's solved already. Didn't get it from last message.
0
A͢J it print nothing
0
So loop from 1 to N*N. No need for nested loops.
0
Emerson Prado but I need to be print it in a tabular form
0
Emerson Prado how can I break line of input can you give me the solution
0
You can add a line break - '\n' - where needed
0
Emerson Prado but it's just for input 4
It's not a generic code for every input
0
TINKLE DASH The function you wrote is generic - it has parameter N exactly for that.
But what difference does it make?
0
Emerson Prado the pattern what I want is
****
****
****
****
But I get here
*
**
***
****
For that I try to rectify inside the function
count=0;
for(i=1;i<=N*N;i+=N){
count+=1;
let s = "";
for(j=i;j<=count*N;j++){
s = s+j+" ";
}
console.log(s);
}
here I solve this
0
You're quite close to the solution, but the communication is being incomplete. So let's do it the right way:
1. Save this code in Code Playground
2. Include a link to it in the question description
3. Include the whole output in the question description
4. Explain your current difficulty in the question description
5. Remove all code from the question description
Then, I can see exactly where you are and suggest the next step.
0
Yup 😊