Why can't I swap characters in a javascript string?

后端 未结 4 1390
無奈伤痛
無奈伤痛 2020-12-11 02:35

I am trying to swap first and last characters of array.But javascript is not letting me swap. I don\'t want to use any built in function.

function swap(arr         


        
4条回答
  •  旧时难觅i
    2020-12-11 03:22

    Let me offer my side of what I understood: swapping items of an array could be something like:

    var myFish = ["angel", "clown", "mandarin", "surgeon"];
    var popped = myFish.pop();
    myFish.unshift(popped) // results in ["surgeon", "angel", "clown", "mandarin"]
    

    Regarding swaping first and last letters of an strings could be done using Regular Expression using something like:

    "mandarin".replace(/^(\w)(.*)(\w)$/,"$3$2$1")// outputs nandarim ==> m is last character and n is first letter
    

提交回复
热议问题