Guest User

Untitled

a guest
Jan 17th, 2019
73
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.08 KB | None | 0 0
  1. // Flip every pair of characters in a string.
  2.  
  3.  
  4. // Example:
  5. // var input = 'check out how interesting this problem is, it\'s insanely interesting!';
  6. // var output = flipPairs(input);
  7. // console.log(output); // --> hcce kuo toh wnietertsni ghtsip orlbmei ,si 't sniasenyli tnreseitgn!
  8. // input = string
  9. // output = string
  10.  
  11. function flipPairs(string) {
  12. var flipArray = [];
  13. //Iterate over the characters of the string increment : 2
  14. for (var i = 1; i < string.length; i += 2) {
  15. flipArray.push(string[i]);
  16. flipArray.push(string[i - 1]);
  17. }
  18. if (string.length % 2 === 1) {
  19. flipArray.push(string[string.length-1]);
  20. }
  21. return flipArray.join('');
  22. }
  23.  
  24. var input = 'check out how interesting this problem is, it\'s insanely interesting!';
  25. var output = flipPairs(input);
  26. console.log(output);
  27.  
  28. function assertEqual(actual, expected, testName) {
  29. if (actual === expected) {
  30. console.log('passed');
  31. } else {
  32. console.log('FAILED [' + testName + ']');
  33. }
  34. }
  35.  
  36. console.log(assertEqual(output, "hcce kuo toh wnietertsni ghtsip orlbmei ,si 't sniasenyli tnreseitgn!", 'String Equality'));
Add Comment
Please, Sign In to add comment