Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 31 additions & 0 deletions 9.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
export function remainingOrders(timeLeft, orders) {
let juice = [];
let time = 0;
Copy link
Member

@Ifycode Ifycode Jun 12, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sorry, my reply is coming a bit late. My Friday and Saturday were filled with coding and debugging other people's code. Plus David came over and stole some of the time I would have used to try it out.

Check out this answer that works, finally @Danbaba1. I've also tested it on
Exercism. It turned out to be a very very very annoying question to solve, though it appeared simple.

I also have figured out a way to use the while loop and switch statement in there.

export function remainingOrders(timeLeft, orders) {
  let totalTime = 0;
  let index = 0;

  while(index < orders.length) {
    let time = 0;
    switch(orders[index]) {
      case 'Pure Strawberry Joy':
      time = 0.5;
      break;
    case 'Energizer':
      time = 1.5;
      break;
    case 'Green Garden':
      time = 1.5;
      break;
    case 'Tropical Island':
      time = 3;
      break;
    case 'All or Nothing':
     time = 5;
      break;
    default:
      time = 2.5;
    }

    totalTime += time;

    if (totalTime - timeLeft >= 0) {
      orders.splice(0, index + 1);
      break;
    }
    
    index += 1;
  }

  if (totalTime < timeLeft) orders = [];

  return orders;
}

PS: If I don't send a video link as promised, I'll just explain on the call we'll be having today. I'll send an invite for 3:30pm.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

okay ma

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@Ifycode the code worked

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@Ifycode i am unable to run this code with a forEach loop and forOf loop correctly.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Always paste your code so that you can get a better review @Danbaba1
To format your code in an issue or github comment, surround your code with 3 or 4 backticks on top and bottom:

```
// Your code goes here

```

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

okay ma

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

function remainingOrders(timeLeft,orders){
  let totalTime = 0;
  let time = 0;
    orders.forEach((element, index) => {
        switch(element){
            case 'Pure Strawberry Joy':{
            time += 0.5;
            break;
            }
          case 'Energizer':{
            time += 1.5;
            break;
          }
          case 'Green Garden':{
            time += 1.5;
            break;
          }
          case 'Tropical Island':{
            time += 3;
            break;
          }
          case 'All or Nothing':{
           time += 5;
            break;
          }
          default:
            time += 2.5;
          }
          if(time - timeLeft >= 0){
            orders.splice(0,index + 1);
          }
          index += 1;
    });
    if(totalTime < timeLeft) orders = [];
    return orders;
}
remainingOrders(5, ['Energizer', 'All or Nothing', 'Green Garden']);

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I see the problem. I don't think you can use a forEach loop for this problem. Remember that when I was using the while loop, I had a break statement inside the if statement block - which means that it will stop the first time the condition time - timeLeft >= 0 is met.
We still need that break, but you can't achieve that with a forEach loop. That's because by default, foreach loop doesn't allow break or continue. You can find time to read about it here - foreach does not allow break

So try this again using normal for loop. Since that allows you to have the break word inside an if statement. I'll check here again by tomorrow evening to see if you get it.

Copy link
Author

@Danbaba1 Danbaba1 Jun 14, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@Ifycode

function remainingOrders(timeLeft,orders){
  let totalTime = 0;
  let index = 0;
  for(let element of orders){
    let time = 0;
    switch(element){
      case 'Pure Strawberry Joy':
      time = 0.5;
      break;
    case 'Energizer':
      time = 1.5;
      break;
    case 'Green Garden':
      time = 1.5;
      break;
    case 'Tropical Island':
      time = 3;
      break;
    case 'All or Nothing':
     time = 5;
      break;
    default:
      time = 2.5;
    }
    totalTime += time;
    //console.log(totalTime);
    if(totalTime - timeLeft >= 0){
      orders.splice(0,index+1);
      break;
    }
    index += 1;
    //console.log(index);
  }
  if(totalTime < timeLeft) orders = [];
return orders;
}
console.log(remainingOrders(5, ['Energizer', 'All or Nothing', 'Green Garden']));

do{
let order = 'Pure Strawberry Joy';
switch(order){
case 'Pure Strawberry Joy':
time = 0.5;
break;
case 'Energizer':
time += 1.5;
break;
case 'Green Garden':
time = 1.5;
break;
case 'Tropical Island':
time = 3;
break;
case 'All or Nothing':
time = 5;
break;
default:
time = 2.5;
}
if((timeLeft - time) == 0){
let name = orders.pop();
juice.push(name);
}
} while(timeLeft > 0);
return juice;
}