Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- function solve (arr, en, type) {
- let priceRat = arr;
- let entrPoint = arr[en];
- let typeOfItems = type;
- // console.log(priceRat);
- let left = priceRat.slice(0, en);
- let right = priceRat.slice(en + 1, );
- let sumLeft = 0;
- let sumRight = 0;
- // console.log(left, right);
- if (typeOfItems === 'cheap') {
- let left2 = left.filter(el => el < entrPoint ? el : false);
- let right2 = right.filter(el => el < entrPoint ? el : false);
- if (left2.length > 0) {
- sumLeft = left2.reduce((acc, el) => acc + el);
- }
- if (right2.length > 0) {
- sumRight = right2.reduce((acc, el) => acc + el);
- }
- } else if (typeOfItems === 'expensive') {
- let left2 = left.filter(el => el >= entrPoint ? el : false);
- let right2 = right.filter(el => el >= entrPoint ? el : false);
- if (left2.length > 0) {
- sumLeft = left2.reduce((acc, el) => acc + el);
- }
- if (right2.length > 0) {
- sumRight = right2.reduce((acc, el) => acc + el);
- }
- }
- // console.log(sumLeft, sumRight);
- let position = '';
- let finalSum = 0;
- if (sumLeft > sumRight) {
- position = 'Left';
- finalSum = sumLeft;
- } else if (sumLeft < sumRight) {
- position = 'Right';
- finalSum = sumRight;
- } else if (sumLeft === sumRight) {
- position = 'Left';
- finalSum = sumLeft;
- }
- console.log(`${position} - ${finalSum}`);
- }
- // solve (
- // [1, 5, 1],
- // 1,
- // "cheap"
- // );
- solve (
- [5, 10, 12, 5, 4, 20],
- 3,
- 'cheap'
- );
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement