fork download
  1. const correctOutput = [
  2. '50556 56550',
  3. '3469798283 8382796934',
  4. '193413442071 714203434119',
  5. '173246791 917463217'
  6. ];
  7.  
  8. const input = [
  9. '5 56 50',
  10. '79 82 34 83 69',
  11. '420 34 19 71 341',
  12. '17 32 91 7 46'
  13. ];
  14.  
  15. let getMax, getMin, workInput;
  16.  
  17. getMax = function(ints) {
  18. let currentIntStr, nextIntStr, temp;
  19.  
  20. for (let i = 0; i < ints.length - 1; i++) {
  21. currentIntStr = ints[i].toString();
  22. nextIntStr = ints[i + 1].toString();
  23. if (
  24. parseInt(currentIntStr + nextIntStr) < parseInt(nextIntStr +
  25. currentIntStr)
  26. ) {
  27. temp = ints[i];
  28. ints[i] = ints[i + 1];
  29. ints[i + 1] = temp;
  30. if (i > 0)
  31. i -= 2;
  32. }
  33. }
  34.  
  35. return ints.join('');
  36. };
  37.  
  38. getMin = function(ints) {
  39. let currentIntStr, nextIntStr, temp;
  40.  
  41. for (let i = ints.length - 1; i > 0; i--) {
  42. currentIntStr = ints[i].toString();
  43. nextIntStr = ints[i - 1].toString();
  44. if (
  45. parseInt(currentIntStr + nextIntStr) < parseInt(nextIntStr +
  46. currentIntStr)
  47. ) {
  48. temp = ints[i];
  49. ints[i] = ints[i - 1];
  50. ints[i - 1] = temp;
  51. if (i < ints.length - 1)
  52. i += 2;
  53. }
  54. }
  55.  
  56. return ints.join('');
  57. };
  58.  
  59. workInput = function() {
  60. let ints, output;
  61.  
  62. for (let i = 0; i < input.length; i++) {
  63. ints = input[i].split(' ');
  64. output = getMin(ints) + ' ' + getMax(ints);
  65. console.log(`[${input[i]}] ${output}: ${output === correctOutput[i] ? 'right' : 'wrong'}`);
  66. }
  67. };
  68.  
  69. workInput();
Success #stdin #stdout 0.03s 122240KB
stdin
Standard input is empty
stdout
[5 56 50] 50556 56550: right
[79 82 34 83 69] 3469798283 8382796934: right
[420 34 19 71 341] 193413442071 714203434119: right
[17 32 91 7 46] 173246791 917463217: right