Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- const weight = x => x.split('').reduce((p,c) => p + +c, 0);
- const compare = (a, b) => a.weight == b.weight ? a.value > b.value : a.weight - b.weight;
- const orderWeight = str => str.trim()
- .split(' ')
- .map(x => ({value: x, weight: weight(x)}))
- .sort(compare)
- .reduce((p,c) => `${p} ${c.value}`,'')
- .trim();
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement