alboig

CombineLists - JavascriptModule0

Apr 14th, 2020
196
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. // Combine Lists
  2. // Write a program that reads two lists of numbers and combines them by alternatingly taking elements:
  3.  
  4. // combine 1,2,3 and 7,8,9 -> 1,7,2,8,3,9
  5. // you can assume that the input lists will have the same length.
  6. // Print the resulting combined list to the output, separating elements with a comma.
  7.  
  8. // Input
  9. // On the first line you will receive the first list.
  10. // On the second line -> 2nd list.
  11. // Output
  12. // On the only line of output, print all the numbers in format n1,n2,n3,..n
  13. // Input
  14. // 2,3,1
  15. // 5,2,3
  16. // Output
  17. // 2,5,3,2,1,3
  18. const input = ['2,3,1', '5,2,3'];
  19. const print = this.print || console.log;
  20. const gets = this.gets || ((arr, index) => () => arr[index++])(input, 0);
  21. const firstArr = gets().split(',');
  22. const secondArr = gets().split(',');
  23. const combinedArr = [];
  24. for (let i = 0; i < firstArr.length; i++) {
  25.   combinedArr.push(firstArr[i]);
  26.   combinedArr.push(secondArr[i]);
  27. }
  28. print(combinedArr.join(','));
Advertisement
Add Comment
Please, Sign In to add comment