Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // Given a string, cycle the first letter of each word back one word,
- // and cycle the last letter of each word forward one word.
- // Example input: "who welld horly"
- // Example output: "why hello world"
- // Do this example by hand:
- // Input: "bes le uoogit"
- // Output: ""
- let recycle = (str) => {
- str = str.split(" ");
- let sol = [];
- for(let i=0; i<str.length; i++){
- 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]);
- }
- return sol.join(" ");
- }
- console.log(recycle("who welld horly"));
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement