0

Output I want to row not column:

var String = "hello world!";
var length = String.length;

for (i=11; i>=0; i--)
{
  console.log (String[i]);
}

What did I miss?

marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
justunknown
  • 55
  • 1
  • 6

1 Answers1

0

You are just logging each character in a string in reverse order instead create a new string and log it.

var String = "hello world!";
var length = String.length;
var res='';

for (i = length - 1; i >= 0; i--) {
  res += String[i];
}
console.log(res);

res = '';
// or while loop
while (length--)
  res += String[length];

console.log(res);

The more simple way is to use String#split, Array#reverse and Array#join method to generate a reversed single string.

var String = "hello world!";

console.log(
  String.split('').reverse().join('')
)
Pranav C Balan
  • 113,687
  • 23
  • 165
  • 188