I want to reverse the string like this: "How are you"
-> "you are How"
Those are the code I typed, it could reverse the st开发者_开发知识库ring ,
but it still has comma between strings like "you,are,How"
var str = window.prompt("Enter a string");
var tokens = str.split( " " );
document.writeln( tokens.reverse() );
Use .join(" ")
var str = window.prompt("Enter a string"); var tokens = str.split( " " );
document.writeln( tokens.reverse().join(" ") );
精彩评论