Ask coding questions

← Back to all posts
How do I get an array to print on only 1 line?
akandrewkoch

Doing the coursework by myself, having a real issue with this one, cannot figure out why the first array cargoHold refuses to print on a single line, while the other two, the cabinets ones, print fine. When I submit it, it says the first print is wrong, but I cannot figure out what I'm doing different with the two console.log commands. I even tried deleting the original let line and retyping it, and it's still the same.
Thanks!

Voters
akandrewkoch
Comments
hotnewtop
akandrewkoch

And now it passed! Thanks, brute force programming at its finest!

akandrewkoch

Thanks, @MrEconomical and @AdCharity, pretty sure that's not how they wanted me to do it, but I love the workaround. Grading system shouldn't know the difference. :)

AdCharity

@akandrewkoch oh well good luck in the future :P

MrEconomical

try console.log(JSON.stringify(thing you want to print))

AdCharity

@MrEconomical really bad idea but you can make a variable that joins the array together into a string and therefore one line.
let str = array.join(“, ”)

MrEconomical

@AdCharity let str = `[${array.join(", ")}]`

AdCharity

@MrEconomical illusion of array complete

akandrewkoch

thanks for the answer, but unless i'm misunderstanding something, that doesn't seem to be it. I just made the console window full screen and ran it again, and it still prints every index item on a different line. It's almost like I have a \n after each item during the printout. Can't figure out why.

[deleted]

The terminal window is wrapping the console.log print out to fit the width of the window. Before you click the Run button widen the window and you should see a difference.