This forum is now read-only. Please use our new forums! Go to forums

0 points
Submitted by Godric Void
almost 12 years

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

points
Submitted by marco_105
over 11 years

1 comments

Sebastian Mihalache over 11 years

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…

points
Submitted by bamwebdesign
over 11 years

4 comments

bamwebdesign over 11 years

not sure what that “if” statement was about…

Denis over 11 years

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

bamwebdesign over 11 years

it let me pass

Michele Moneywell over 11 years

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…

points
Submitted by bamwebdesign
over 11 years

1 comments

Wayne Loke over 11 years

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) }

points
Submitted by jorjphoenix
over 11 years

2 comments

Anindito Setya over 11 years

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); }

points
Submitted by Nikita Alshinsky
over 11 years

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.

points
Submitted by James Skevington
over 11 years

1 comments

Wayne Loke over 11 years

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); }

points
Submitted by Igor Kuznetsov
over 11 years

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);

}

points
over 11 years

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); }

points
Submitted by Diego Feijó
over 11 years

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);

}

points
over 11 years

Answer 4fbc56a88a811c000300e989

-2 votes

Permalink

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

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

points
Submitted by jfroman88
almost 12 years

4 comments

Arya Prakash over 11 years

he is NEVER WRONG

bamwebdesign over 11 years

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

Michele Moneywell over 11 years

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

bamwebdesign over 11 years

yes but c<cells is shorter