Skip to content

Instantly share code, notes, and snippets.

@tdlm
Created February 18, 2017 23:19
Show Gist options
  • Save tdlm/7dc1a83178305050a72b7b531c9521c7 to your computer and use it in GitHub Desktop.
Save tdlm/7dc1a83178305050a72b7b531c9521c7 to your computer and use it in GitHub Desktop.
const arrange = (str) => {
let words = str.split(' '), i = 0;
for (; i < words.length - 1; i++) {
if (
(0 === i % 2 && words[i].length > words[i + 1].length) ||
(1 === i % 2 && words[i].length < words[i + 1].length)
) {
let tmp = words[i];
words[i] = words[i + 1];
words[i + 1] = tmp;
}
}
return words.map(function(word, i) {
return i % 2 == 0 ? word.toLowerCase() : word.toUpperCase();
}).join(' ');
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment