0

I kind of feel like and idiot asking this but learning javascript and was doing easy challenges on coderbyte and wanted to reverse a string so worte this

var randomString = "THIS IS THE STRING I AM TRYING TO REVERSE"; 
 var newFunc = function() {
 var randomArray = randomString.split(" ");
 var newArray = [];
 for (var i = 0; i <= randomArray.length; i++) {
 newArray[i] = randomArray.pop();
 var diffString = newArray.join(" ");
 }
 console.log(diffString);
 };
 newFunc();`

But the output I get is just

"REVERSE TO TRYING AM I".

Why is not printing the rest of the string? I am now working on reversing it with a different method but this was the most straightforward and just confused why it didn't work. Thanks in advance.

RobG
148k32 gold badges180 silver badges216 bronze badges
asked Jan 14, 2016 at 1:23
4
  • 1
    the issue is simple, you are reducing original array's length whenever you call pop() Commented Jan 14, 2016 at 1:25
  • retrieve array length before hand, also take diffString outside for loop... ( P. S: a solution in a line might be using split-> reverse -> join), but I dont wanna give you the answer, think about it... Commented Jan 14, 2016 at 1:26
  • 1
    LIke mido said, the pop did it. The solution can be as simple as this: randomString.split(' ').reverse().join(' '); Commented Jan 14, 2016 at 1:27
  • Could also do randomString.split(' ').reduceRight(function(a,v){a.push(v);return a;},[]).join(' '); but split/reverse/join is probably faster. ;-) Commented Jan 14, 2016 at 2:06

3 Answers 3

1

When you pop values off randomArray, it is changing the length of the array. Since your for loop checks it on every iteration, it will break out of the loop prematurely because the length gets shorter.

The fix is to save the length before you start popping, and use that in the for loop.

(This assumes you are trying to reverse the order of the words in your string.)

var randomString = "THIS IS THE STRING I AM TRYING TO REVERSE"; 
var newFunc = function() {
 var randomArray = randomString.split(" ");
 var newArray = [];
 var length = randomArray.length;
 for (var i = 0; i <= length; i++) {
 newArray[i] = randomArray.pop();
 }
 var diffString = newArray.join(" ");
 console.log(diffString);
};
newFunc();
Ted A.
2,30217 silver badges22 bronze badges
answered Jan 14, 2016 at 1:29
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks, that was helpful and this var length = randomArray.length; was just cool and didn't know you could do that. I am still looking through the answers everybody else provided and learning more. I actually went through the loop on paper and that's when I realized the length gets shorter.
1

As others have noted, checking the changing length of the array is a problem; cache it once up front, so you pop the correct number of times, just change the for loop to (also fixing from <= to correct <):

for (var i = 0, len = randomArray.length; i < len; i++) {

Of course, the silly part here is that you're hand-implementing reversal. There's already Array.reverse:

var newFunc = function() {
 console.log(randomString.split(" ").reverse().join(" "));
};

Done. One-lined even. And it's not like the function is non-standard, IE supports it since 5.5, and every other browser since their 1.0 release.

answered Jan 14, 2016 at 2:34

Comments

0

As mentioned before you are changing the length during your loop. Maybe Array.sort would be easier to use here? Or... well Array.reverse() as ShadowRanger mentioned.

var randomString = "test sds2 iujn3 iujn4 jijb5 reverse";
function reverse(x) {
 return x // This is a <string>
 .split(" ") // Transform <string> into <Array>
 .reverse() // Reverse array order
 .join(" "); // Transform <Array> back into <string>
}
console.log(reverse(randomString));

answered Jan 14, 2016 at 2:26

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.