-1

I'm trying to reverse a string, and it does work, but when I store the userInput[i] into the result variable, I'm getting NaN at the end of the string.

//variables
var userInput, result;

userInput = prompt("Enter a string that you want reversed: ");

for (var i = userInput.length; i >= 0; i--) {
    result += userInput[i];
}

console.log("Your string: \n" + result);
Dipiks
  • 3,818
  • 2
  • 23
  • 39

6 Answers6

3

You start the loop from userInput.length which is a wrong index because userInput[userInput.length] is undefined. What you need to do is start from userInput.length - 1 like this (and don't forget to initialize result to empty string before starting to accumulate the result):

result = "";
for (var i = userInput.length - 1; i >= 0; i--) {
  result += userInput[i];
}

NOTE: When going up an array, we don't actually reach the point when we access the userInput.length index (i.e. userInput[userInput.length]) because usually the condition for the loop is i < userInput.length which fail as soon as i is equal to userInput.length. When going downwards an array, one should keep track of what are the indexes allowed.

NOTE 2: It is safer to use string.charAt(index) rather than string[index]. Learn why here.

Community
  • 1
  • 1
Kaushik solanki
  • 438
  • 3
  • 15
2

You are better off, by reversing a string in JavaScript using something like below:

'YOURSTRING'.split('').reverse().join('');

e-g

'abcdef'.split('').reverse().join('') will give you fedcba

defau1t
  • 10,593
  • 2
  • 35
  • 47
1

try this

    var userInput, result = ""; // initialize this first
    
    userInput = prompt("Enter a string that you want reversed: ");
    
    for (var i = userInput.length - 1; i >= 0; i--) {
      result += userInput[i];
    }
    console.log("Your string: \n" + result);
Ameya Deshpande
  • 3,580
  • 4
  • 30
  • 46
1

this problem consists of 2 smaller problems.

First problem:

"result" is not initialized so it contains "undefined" which messes with the string later on. Initialize it to be an empty string so JavaScript knows it should handle it as a string since the "+"-operator is overloaded to handle addition and string concatenation.

Second problem

The index of the userInput string reaches from 0 - (userInput.length - 1), so change the loop accordingly.

var userInput, result = ""; // initialize this first

userInput = prompt("Enter a string that you want reversed: ");

// initialize i with (userInput.length - 1) since the index ends there
for (var i = userInput.length - 1; i >= 0; i--) {
  result += userInput[i];
}
console.log("Your string: \n" + result);
1

The length property of String returns the letters' count whereas the index of an Array starts from 0. So, your issue could be resolved by reducing the string length by 1 i.e. userInput.length - 1.

var userInput, result="";

userInput = prompt("Enter a string that you want reversed: ");

for (var i = userInput.length - 1; i >= 0; i--) {
  result += userInput[i];
}
console.log("Your string: \n" + result);
Clyde Lobo
  • 9,126
  • 7
  • 34
  • 61
0

Get you string.Split it into array.reverse your array and join.Finally you will get the reversed string.

 var userInput, result = ""; // initialize this first
    
    userInput = prompt("Enter a string that you want reversed: ");
    
   result = userInput.split("").reverse().join("");
    console.log("Reversed string: \n" + result);
Hikmat Sijapati
  • 6,869
  • 1
  • 9
  • 19