[배열] string 거꾸로 출력하기

2020. 5. 21. 14:22컴퓨터언어/자료구조&알고리즘

728x90
반응형
function reverse(str){
  if (!str || str.length<0) {
    return "Get off, not a string dude."
  }
  let go = "";
  for (let i=str.length-1; i>=0; i--) {
    go += str[i]
  }
  return go
}
reverse("Hi My Name Is John")

const reverse2 = str => [...str].reverse().join("")
reverse2("Even more concise")
728x90
반응형