Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Mixed Juices Exercism #4

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open

Conversation

Danbaba1
Copy link

@Danbaba1 Danbaba1 commented Jun 7, 2022

Attempt at function 3 on mixed juices Exercise.

Attempt at function 3 on mixed juices Exercise.
@Ifycode
Copy link
Member

Ifycode commented Jun 7, 2022

@Danbaba1 I've seen it. Sorry I haven't been able to take a look at it for now, I have a deadline to meet at work. I will check it out for you tomorrow (afternoon or evening).

@Danbaba1
Copy link
Author

Danbaba1 commented Jun 8, 2022 via email

@@ -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']));

@Ifycode
Copy link
Member

Ifycode commented Jun 12, 2022

Or better still @Danbaba1 , reuse the time to mix function (i.e function 1) inside function 3 in this manner:

Where function 1 assigns the time to the juices:

export function timeToMixJuice(name) {
  let time = 0;
  switch(name) {
      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;
    }

  return time;
}

And function 3 just calls it when it needs the time:

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

  while(index < orders.length) {
    let time = timeToMixJuice(orders[index]);

    totalTime += time;

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

  if (totalTime < timeLeft) orders = [];

  return orders;
}

This validates my point when I said you should just assign inside the switch statement and not attempt to increment inside there.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants