Advertisement
ErolKZ

Untitled

Oct 24th, 2021
84
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.56 KB | None | 0 0
  1.  
  2. function solve (arr, en, type) {
  3.  
  4.  
  5. let priceRat = arr;
  6.  
  7. let entrPoint = arr[en];
  8.  
  9. let typeOfItems = type;
  10.  
  11. // console.log(priceRat);
  12.  
  13. let left = priceRat.slice(0, en);
  14.  
  15. let right = priceRat.slice(en + 1, );
  16.  
  17. let sumLeft = 0;
  18.  
  19. let sumRight = 0;
  20.  
  21. // console.log(left, right);
  22.  
  23.  
  24. if (typeOfItems === 'cheap') {
  25.  
  26. let left2 = left.filter(el => el < entrPoint ? el : false);
  27.  
  28. let right2 = right.filter(el => el < entrPoint ? el : false);
  29.  
  30. if (left2.length > 0) {
  31.  
  32. sumLeft = left2.reduce((acc, el) => acc + el);
  33.  
  34. }
  35.  
  36. if (right2.length > 0) {
  37.  
  38. sumRight = right2.reduce((acc, el) => acc + el);
  39.  
  40. }
  41.  
  42. } else if (typeOfItems === 'expensive') {
  43.  
  44.  
  45. let left2 = left.filter(el => el >= entrPoint ? el : false);
  46.  
  47. let right2 = right.filter(el => el >= entrPoint ? el : false);
  48.  
  49. if (left2.length > 0) {
  50.  
  51. sumLeft = left2.reduce((acc, el) => acc + el);
  52.  
  53. }
  54.  
  55. if (right2.length > 0) {
  56.  
  57. sumRight = right2.reduce((acc, el) => acc + el);
  58.  
  59. }
  60.  
  61.  
  62.  
  63. }
  64.  
  65.  
  66.  
  67. // console.log(sumLeft, sumRight);
  68.  
  69. let position = '';
  70.  
  71. let finalSum = 0;
  72.  
  73.  
  74. if (sumLeft > sumRight) {
  75.  
  76. position = 'Left';
  77.  
  78. finalSum = sumLeft;
  79.  
  80. } else if (sumLeft < sumRight) {
  81.  
  82. position = 'Right';
  83.  
  84. finalSum = sumRight;
  85.  
  86. } else if (sumLeft === sumRight) {
  87.  
  88. position = 'Left';
  89.  
  90. finalSum = sumLeft;
  91.  
  92. }
  93.  
  94.  
  95. console.log(`${position} - ${finalSum}`);
  96.  
  97.  
  98. }
  99.  
  100.  
  101. // solve (
  102.  
  103. // [1, 5, 1],
  104.  
  105. // 1,
  106.  
  107. // "cheap"
  108.  
  109. // );
  110.  
  111.  
  112. solve (
  113.  
  114. [5, 10, 12, 5, 4, 20],
  115.  
  116. 3,
  117.  
  118. 'cheap'
  119.  
  120. );
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement