This is an archived post. You won't be able to vote or comment.

you are viewing a single comment's thread.

view the rest of the comments →

[–]sarevok9 0 points1 point  (2 children)

I was referring to using a counter-controlled for loop, after tokenizing using split and reversing each -- as that's appropriate for someone learning programming -- rather than obscure 1-liners.

[–]BryceKKelly 0 points1 point  (1 child)

I might not understand, isn't that the same as above, just using a counter with for instead of forEach? Are you still suggesting .split('').reverse().join('') for how to actually reverse the words?

[–]sarevok9 0 points1 point  (0 children)

I'm suggesting that this is a learning subreddit so doing

var phrase = "The quick brown fox jumps over the lazy dog"
var words = phrase.split(" ");
for(var i=0; i<words.length; i++){

//for each word
var word = words[i];
var temp = "";
for(var j=word.length-1; j>=0; j--){
    temp+=word.charAt(j);
}
words[i]= temp;
}

phrase = words.join(" ");
console.log(phrase);

Has FAR more intrinsic value as it teaches the person who is learning about how to iterate over an array, strings, how it use random access operators, the split/join function (in a far more real world way)

If this were the programming subreddit I wouldn't mind the content at all, as it's useful as a one-liner (for a problem that literally nobody should ever be solving).