Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // Flip every pair of characters in a string.
- // Example:
- // var input = 'check out how interesting this problem is, it\'s insanely interesting!';
- // var output = flipPairs(input);
- // console.log(output); // --> hcce kuo toh wnietertsni ghtsip orlbmei ,si 't sniasenyli tnreseitgn!
- // input = string
- // output = string
- function flipPairs(string) {
- var flipArray = [];
- //Iterate over the characters of the string increment : 2
- for (var i = 1; i < string.length; i += 2) {
- flipArray.push(string[i]);
- flipArray.push(string[i - 1]);
- }
- if (string.length % 2 === 1) {
- flipArray.push(string[string.length-1]);
- }
- return flipArray.join('');
- }
- var input = 'check out how interesting this problem is, it\'s insanely interesting!';
- var output = flipPairs(input);
- console.log(output);
- function assertEqual(actual, expected, testName) {
- if (actual === expected) {
- console.log('passed');
- } else {
- console.log('FAILED [' + testName + ']');
- }
- }
- console.log(assertEqual(output, "hcce kuo toh wnietertsni ghtsip orlbmei ,si 't sniasenyli tnreseitgn!", 'String Equality'));
Add Comment
Please, Sign In to add comment