0

The function print() gives newline character at the end.

following is the JS code

print("first");
Things =["car","building"]
for (var i = Things.length - 1; i >= 0; i--) 
{
    print(Things[i])
}

the Output I get for the code above is

first
building
car

Whereas I intended to print like this

firstbuildingcar

How do I print them in a single line using print() function?

Kumar Sidharth
  • 578
  • 5
  • 13
  • Possible duplicate of [printing output same line using console log in javascript](http://stackoverflow.com/questions/28620087/printing-output-same-line-using-console-log-in-javascript) – dot.Py May 18 '16 at 17:03

2 Answers2

0

Create an array and join it using a blank delimiter:

print(['uno','dos','tres'].join("")); // outputs "unodostres"

You don't need to loop. You do need to create your array before joining it (whereas you're printing one standalone string that's not part of an array, and then looping over your array).

If you want to reverse the print order of your array-- which it looks like your loop is doing-- just reverse() it before you join():

print(['uno','dos','tres'].reverse().join("")); // outputs "tresdosuno"
Marc
  • 11,403
  • 2
  • 35
  • 45
0
Things =["car","building"]
res ="first"
for (var i = Things.length - 1; i >= 0; i--) 
{
    res=res+Things[i]
}
print(res);
Andrea Sindico
  • 7,358
  • 6
  • 47
  • 84