0

I'm trying to do a reverse-word script in javascript. I have written this function, but it does not work and I can't understand why. What is wrong? Is there any other way to do it?

function inverser(x)
{
    var newString ="";
    var index=(x.length - 1);
    var y=0;
    
    for (var i=index ; i>=0 ; i--)
    {
        x[i] = newString [y++];
    }
    
    return (newString );
}

console.log(inverser("test"));
Stacks Queue
  • 848
  • 7
  • 18
PNB
  • 75
  • 10
  • 2
    Possible duplicate of [How do you reverse a string in place in JavaScript?](http://stackoverflow.com/questions/958908/how-do-you-reverse-a-string-in-place-in-javascript) – Alex May 07 '17 at 16:02
  • logic is totally backwards – charlietfl May 07 '17 at 16:02
  • Your assignment is in reverse. And strings are immutable, you cannot assign to their indices at all. Use an array instead, or build the string through concatenation. – Bergi May 07 '17 at 16:02
  • 1
    "but it does not work" isn't a very helpful description of your problem. What *is* the output for a given input? And what have you done to debug it? – Carcigenicate May 07 '17 at 16:02
  • It should be: `newString [y++] = x[i];` not `x[i] = newString [y++];` – Karim Harazin May 07 '17 at 16:02

7 Answers7

1

You need to add to concatenate newString instead of trying to assign an index to it. The logic of your function should be

newString[y++] += x[i];

Link to code

Garrett Kadillak
  • 1,026
  • 9
  • 18
0

Use reverse():

function inverser(x)
{   
    return x.split("").reverse().join("");
}

It's simple if you don't need build in function, please comment, i will update my answer :)

Update

The assignment statements are wrong in your code, it should have been:

newString += x[i];
Abishek V Ashok
  • 513
  • 3
  • 17
0

when you use newString[y++], newString is still empty string and doesn't have cell 1 or 2 or 3 .... and can't assign string to this cell.

simply if you remove y variable and make some move on line 9 can get answer.

here the code worked for me:

function inverser(x) {
  var newString = "";
  var index = (x.length - 1);

  for (var i = index; i >= 0; i--)
  {
    newString += x[i];
  }

  return (newString);
}
farbod
  • 91
  • 7
0
"use strict";
function wordReverse() {
  const str1 = "God is Good";
  const str2 = str1.split(" ");
  //console.log(str2);
  const reverseWord = [];
  const len = str2.length;
  //console.log(len);
  for (let i = len - 1; i >= 0; i--) {
    reverseWord.push(str2[i]);
  }
  return reverseWord;
}
console.log(wordReverse().join(" "));
0
const word = "reverse";
const reverseString = (str) => str.split("").reverse().join("");
reverseString(word);

enter image description here

Eran Peled
  • 767
  • 6
  • 6
0
function reverseWord(str) {
    const splittedString = str.split(" ");
    const wordreverse = [];
    const len = splittedString.length;

    for (let i = len - 1; i >= 0; i--) {
            wordreverse.push(splittedString[i]);
    }
    return wordreverse.join(" ");
}
0
function reverseWord(str) {
    const splittedString = str.split(" ");
    const wordreverse = [];
    const len = splittedString.length;

    for (let i = len - 1; i >= 0; i--) {
            wordreverse.push(splittedString[i]);
    }
    return wordreverse.join(" ");
}
console.log(reverseWord("hello world"))