Need help for javascript code

Joined
Sep 28, 2022
Messages
2
Reaction score
0
I want a help about my code because i can't have an output and I don't know where's the error

let arr = [[1, 2, 3], [4, 5, 6], [7, 8, 9]];
console.log(arr);
for(let i = arr.length; i >= 0; i--){
console.log(arr);
for(let j = arr.length; j >= 0; j--){
console.log(arr[j]);
}
}
 
Joined
Feb 12, 2020
Messages
8
Reaction score
0
in your second for loop, it should be:

JavaScript:
for(let j  = arr[i].length; j>=0; j--){
    console.log(arr[i][j]);
}

Instead of:

Code:
for(let j  = arr.length; j>=0; j--){
    console.log(arr[j]);
}
 
Joined
Sep 28, 2022
Messages
2
Reaction score
0
in your second for loop, it should be:

JavaScript:
for(let j  = arr[i].length; j>=0; j--){
    console.log(arr[i][j]);
}

Instead of:

Code:
for(let j  = arr.length; j>=0; j--){
    console.log(arr[j]);
}
Yeah but when i rectify my code as you said
I have the code below
JavaScript:
let arr = [[1, 2, 3], [4, 5, 6], [7, 8, 9]];
console.log(arr);
for(let i = arr.length; i >= 0; i--){
console.log(arr[i]);
for(let j = arr[i].length; j >= 0; j--){
console.log(arr[i][j]);
}
}

But I still have error
 
Joined
Feb 12, 2020
Messages
8
Reaction score
0
Try this:


JavaScript:
let arr = [[1, 2, 3], [4, 5, 6], [7, 8, 9]];
for(let i = arr.length; i > 0; i--){
    console.log(arr[i-1])
    for(let j =  arr[i-1].length; j > 0; j--){
        console.log(arr[i-1][j-1])
    }
}
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,828
Messages
2,569,736
Members
45,518
Latest member
vlonesshirt

Latest Threads

Top