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?
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?
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('')
)