Close Circle Icon
This forum is now read-only. Please use our new forums at discuss.codecademy.com.
0 points
Submitted by Godric Void
8 years ago

Printing out tables (Part 2)

var table = [
["Person", "Age", "City"], 
["Sue", 22, "San Francisco"], 
["Joe", 45, "Halifax"] ];

for(var data in table){
var c; var cells = table.length; var rowText = "";

for(c=0;c<cells;c++){ 

if(c < cells-1){
    rowText +=  table[data][c] + "  "; 
}
else{
    rowText +=  table[data][c];
}
}
console.log(rowText);

}

Answer 5019e82437152a000200de29

4 votes

Permalink

No, no, the problem is that we follow the hint comment which is not complete…,
look about the enonciate and you will find the error between them !

There should be two spaces between each

so you fill it and it’s done !
if(c < cells-1){
rowText += table[data][c] + “ “ + “ “ ;

codeAcademy sometime very subtil…

NJS

965 points
Submitted by marco_105
8 years ago

1 comments

Sebastian Mihalache 8 years ago

var rows = table.length; for(var r = 0; r < rows; r++){ var c; var cells = table[r].length; var rowText = “”; for (c=0; c<cells; c++){ rowText += table[r][c]; if (c < cells-1){ rowText += “ “+” “; } } console.log(rowText); }

  • The above worked fine for me!

Answer 50207d8dd5d1f00002002368

3 votes

Permalink

WARNING SPOILER

I got

var table = [
    ["Person",  "Age",  "City"],
    ["Sue",     22,     "San Francisco"],
    ["Joe",     45,     "Halifax"]
];
var rows = table.length;
for(r=0; r<rows;r++){
    var cells = table.length;
    var rowText = "";
    for(c=0; c<cells; c++){    
    rowText +=  table[r][c] + "  ";     
    }
    console.log(rowText);
}

And it’s working…

766 points
Submitted by bamwebdesign
8 years ago

4 comments

bamwebdesign 8 years ago

not sure what that “if” statement was about…

Denis 8 years ago

great solution. but the machine wont let u pass without an IF

bamwebdesign 8 years ago

it let me pass

Michele Moneywell 8 years ago

You are adding the two spaces each time. You are not supposed to add the two spaces for the last item in the row. That is why you are supposed to add an if statement. If it isn’t the last item, then rowText += “ “

Answer 50207cb06fe4c800020020b8

1 vote

Permalink

I’ve got

   var table = [
    ["Person",  "Age",  "City"],
    ["Sue",     22,     "San Francisco"],
    ["Joe",     45,     "Halifax"]
];
rows = table.length;
for(r=0; r<rows;r++){
    var cells = table.length;
    rowText = "";
    for(c=0; c<cells; c++){
        if(c < cells){
    rowText +=  table[r][c] + "  "; 
}

        console.log(rowText);
    }
}

Which gets me the output:

Person  
Person  Age  
Person  Age  City  
Sue  
Sue  22  
Sue  22  San Francisco  
Joe  
Joe  45  
Joe  45  Halifax  
Oops, try again.

So I’m nearly there, just trying to figure out how to get rid of those extra lines…

766 points
Submitted by bamwebdesign
8 years ago

1 comments

Wayne Loke 8 years ago

Your console.log(rowText) needs to be outside the second loop.

Answer 5022136056102a00020123eb

1 vote

Permalink

##It’s work var table = [ [“Person”, “Age”, “City”], [“Sue”, 22, “San Francisco”], [“Joe”, 45, “Halifax”] ]; var rows=table.length; var r; for (r=0;r<rows;r++){ var c; var cells=table.length; var rowText=”” for (c=0;c<cells;c++){ rowText+=table[r][c] if (c<cells-1){ rowText+=” “ }

} console.log(rowText) }

562 points
Submitted by jorjphoenix
8 years ago

2 comments

Anindito Setya 8 years ago

still can not solve this.. need help… even if i copy and paste the code above, it still say “Oops, try again. “

var table = [ [“Person”, “Age”, “City”], [“Sue”, 22, “San Francisco”], [“Joe”, 45, “Halifax”] ]; var rows = table.length; var r;

for (r=0;r<rows;r++){ var c; var cells = table[r].length; var rowText=””;

for(c=0;c<cells;c++){
if(c!==table[r].length){
        rowText+=table[r][c]+" "+" ";
    }

    }
console.log(rowText);

}

Answer 502abe6ee3313d000201c382

1 vote

Permalink

Thank you. There should be double-spacing.

That what I have (according to the hint):

var table = [ [“Person”, “Age”, “City”], [“Sue”, 22, “San Francisco”], [“Joe”, 45, “Halifax”] ]; var rows = table.length; var r; for (r = 0; r < rows; r++){ var c; var cells = table[r].length; var rowText = “”; for(c = 0; c < cells; c++){ rowText += table[r][c]; if (c < cells-1){ rowText += “ “; } }console.log(rowText);
}

306 points
Submitted by Nikita Alshinsky
8 years ago

Answer 5032462d119ac70002044639

1 vote

Permalink

This was a really interesting exercise, I just wish it was explained better. The hint isn’t exactly a hint, it just tells you what to do.

554 points
Submitted by James Skevington
8 years ago

1 comments

Wayne Loke 8 years ago

Agree. There is a couple of variation that allow you to pass this exercise.

Answer 502400798fdd93000201e038

0 votes

Permalink

var table = [ [“Person”, “Age”, “City”], [“Sue”, 22, “San Francisco”], [“Joe”, 45, “Halifax”] ]; var length = table.length; for (var i = 0; i <= length-1; i++) { var c, cells = table[i].length, rowText = “”; for (c = 0; c <= cells - 1; c++) { if (c === cells - 1) { rowText += table[i][c]; } else { rowText += table[i][c] + “ “; } } console.log(rowText); }

1262 points
Submitted by Igor Kuznetsov
8 years ago

Answer 504d700fa8dac900020038b6

0 votes

Permalink

var table = [ [“Person”, “Age”, “City”], [“Sue”, 22, “San Francisco”], [“Joe”, 45, “Halifax”] ]; var rows = table.length; var r;

for (r=0;r<rows;r++){ var c; var cells = table[r].length; var rowText=””;

for(c=0;c<cells;c++){
if(c!==table[r].length){
        rowText+=table[r][c]+" "+" ";
    }

    }
console.log(rowText);

}

1014 points
8 years ago

Answer 5064836bee293000020b7c3d

0 votes

Permalink

Correct.

var table = [ [“Person”, “Age”, “City”], [“Sue”, 22, “San Francisco”], [“Joe”, 45, “Halifax”] ];

var rows = table.length;

for (var row in table){
//console.log(row); var cells = “”; for ( index = 0; index < table[row].length; index++){ if (index === 1){ cells += “ “ + table[row][index] + “ “; } else{ cells += table[row][index]; } } console.log(cells); }

707 points
Submitted by Diego Feijó
8 years ago

Answer 50681b48e28e93000202cc7b

0 votes

Permalink

That’s correct! Next Exercise: Breaking Out

var table = [ [“Person”, “Age”, “City”], [“Sue”, 22, “San Francisco”], [“Joe”, 45, “Halifax”] ];

var rows = table.length; var r = 0;

for (r = 0; r <= rows -1; r++) { var c; var cells = table[r].length; var rowText = “”; for ( c = 0; c <= cells -1; c++) { rowText += table[r][c];

        if (c < cells -1)
        {
            rowText += " " + " "; 
        }
}
console.log(rowText);

}

863 points
8 years ago

Answer 4fbc56a88a811c000300e989

-2 votes

Permalink

change ….. for(c =0; c <=cells;c++) {

to ……. for (c = 0; c <= cells - 1; c++) {

326 points
Submitted by jfroman88
8 years ago

4 comments

Arya Prakash 8 years ago

he is NEVER WRONG

bamwebdesign 8 years ago

I think you’re after c< cells which is the same as c<= cells-1 …

Michele Moneywell 8 years ago

Both of those statements have the same result in this example.

bamwebdesign 8 years ago

yes but c<cells is shorter