Advertisement
natalyayemelyanova

Павленко Евгений 2

Jan 30th, 2018
83
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.68 KB | None | 0 0
  1. function isPossible (str1, str2) {
  2. var chars1 = str1.split('');
  3. var chars2 = str2.split('');
  4. var length = chars2.length;
  5.  
  6. while (length--) {
  7. var pos = chars1.indexOf(chars2[length]);
  8.  
  9. if (chars2[length] !== ' ') {
  10. if (-1 !== pos) {
  11. //Char exists in source string - remove it
  12. chars1.splice(pos, 1);
  13. } else {
  14. return false;
  15. }
  16. }
  17.  
  18. //Char exists in result string - remove it
  19. chars2.splice(length, 1);
  20. }
  21.  
  22. return true;
  23. }
  24.  
  25. console.log(isPossible('clockwise', 'is clock'));//->true
  26. console.log(isPossible('clockwise', 'is ccc'));//->false
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement