Guest User

Untitled

a guest
Jul 22nd, 2018
71
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.83 KB | None | 0 0
  1. const prepareString = (text) => text.split(' ').join('').split('');
  2.  
  3. const flames = (name1, name2) => {
  4. const flames = ['FRIENDS', 'LOVERS', 'ANGRY', 'MARRIED', 'ENEMIES', 'SOULMATES'];
  5.  
  6. const name1arr = (name1.length < name2.length) ? prepareString(name1) : prepareString(name2);
  7. let nameToCompare = (name1.length < name2.length) ? name2 : name1;
  8.  
  9. const matches = [];
  10. name1arr.forEach(x => {
  11. const indexMatch = nameToCompare.indexOf(x);
  12.  
  13. if (indexMatch > -1) {
  14. matches.push(x);
  15. nameToCompare = nameToCompare.replace(x, '$');
  16. }
  17. });
  18. return {
  19. matches,
  20. flames: flames[(matches.length-1) % 6]
  21. };
  22. }
  23. const result = flames('m a r k h u g h n e ri', 'm a r k h u g h n e r i')
  24. console.log(result);
  25. // { matches: [ 'm', 'a', 'r', 'k', 'h', 'u', 'g', 'h', 'n', 'e', 'r', 'i' ],
  26. // flames: 'SOULMATES' }
Add Comment
Please, Sign In to add comment