LoginSignup
7

More than 5 years have passed since last update.

js 文字列を逆に並べるには?

Last updated at Posted at 2016-12-11

文字列を逆に並べる

function reverseString(str) {
   //write your code.
 }
reverseString("hello");//olleh

出力結果 例

reverseString("hello") // "olleh".
reverseString("Howdy") // "ydwoH".
reverseString("Greetings from Earth") // "htraE morf sgniteerG".

試したコード

function reverseString(str) {
   return str.split("").reverse().join("");
 }
reverseString("hello");//olleh

考え方

split()で文字列を分割//["h", "e", "l", "l", "o"]
reverse()で逆に並べる。//["o", "l", "l", "e", "h"]
join("")で一緒にする。//olleh

もっと簡潔に美しくかけるよ! という方、コメントお待ちしております。

参考リンク

split()
reverse()
join()

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
7