2021年1月22日星期五

swapping the first and last letters of every word in a string using javascript

Given a string of words separated by spaces, write a function that swaps the first and last letters of every word. You may assume that every word contains at least one letter, and that the string will always contain at least one word. You may also assume that each string contains nothing but words and spaces, and that there are no leading, trailing, or repeated spaces.

This is what I have so far

function first_last(str1){   if (str1.length <= 1) {    return str1;   }      mid_char = str1.substring(1, str1.length - 1);    return (str1.charAt(str1.length - 1)) + mid_char + str1.charAt(0);   }   console.log(first_last('Hello there')); //eello therH  
https://stackoverflow.com/questions/65854575/swapping-the-first-and-last-letters-of-every-word-in-a-string-using-javascript January 23, 2021 at 08:36AM

没有评论:

发表评论