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.
3 Answers 3
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();
1 Comment
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.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.
Comments
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));
pop()diffStringoutside 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...randomString.split(' ').reduceRight(function(a,v){a.push(v);return a;},[]).join(' ');but split/reverse/join is probably faster. ;-)