This forum is now read-only. Please use our new forums! Go to forums
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
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
Answer 50207d8dd5d1f00002002368
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…
4 comments
not sure what that “if” statement was about…
great solution. but the machine wont let u pass without an IF
it let me pass
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
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…
1 comments
Your console.log(rowText) needs to be outside the second loop.
Answer 5022136056102a00020123eb
##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) }
2 comments
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
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); }
Answer 5032462d119ac70002044639
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.
1 comments
Agree. There is a couple of variation that allow you to pass this exercise.
Answer 502400798fdd93000201e038
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); }
Answer 504d700fa8dac900020038b6
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 5064836bee293000020b7c3d
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);
}
Answer 50681b48e28e93000202cc7b
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);
}
Answer 4fbc56a88a811c000300e989
change ….. for(c =0; c <=cells;c++) {
to ……. for (c = 0; c <= cells - 1; c++) {
4 comments
he is NEVER WRONG
I think you’re after c< cells which is the same as c<= cells-1 …
Both of those statements have the same result in this example.
yes but c<cells is shorter
Popular free courses
- Free Course
Learn SQL
In this SQL course, you'll learn how to manage large datasets and analyze real data using the standard data management language.Beginner friendly,4 LessonsLanguage Fluency - Free Course
Learn JavaScript
Learn how to use JavaScript — a powerful and flexible programming language for adding website interactivity.Beginner friendly,11 LessonsLanguage Fluency - Free Course
Learn HTML
Start at the beginning by learning HTML basics — an important foundation for building and editing web pages.Beginner friendly,6 LessonsLanguage Fluency
1 comments
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); }