Advertisement
Guest User

Untitled

a guest
Aug 21st, 2019
83
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.62 KB | None | 0 0
  1. // Given a string, cycle the first letter of each word back one word,
  2. // and cycle the last letter of each word forward one word.
  3.  
  4. // Example input: "who welld horly"
  5. // Example output: "why hello world"
  6.  
  7. // Do this example by hand:
  8. // Input: "bes le uoogit"
  9. // Output: ""
  10.  
  11. let recycle = (str) => {
  12. str = str.split(" ");
  13. let sol = [];
  14. for(let i=0; i<str.length; i++){
  15. sol.push(str[i + 1 > str.length - 1 ? 0 : i + 1].charAt(0) + str[i].slice(1, str[i].length - 1) + str[i - 1 < 0 ? str.length - 1 : i - 1][str[i - 1 < 0 ? str.length - 1 : i - 1].length - 1]);
  16. }
  17. return sol.join(" ");
  18. }
  19.  
  20. console.log(recycle("who welld horly"));
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement