Conversation
Attempt at function 3 on mixed juices Exercise.
|
@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). |
|
okay ma thank you.
…On Tue, 7 Jun 2022 at 19:38, Obiagba Mary Ifeoma ***@***.***> wrote:
@Danbaba1 <https://github.com/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).
—
Reply to this email directly, view it on GitHub
<#4 (comment)>,
or unsubscribe
<https://github.com/notifications/unsubscribe-auth/AXRP57QEIP7MDLPARUDDTALVN6JKXANCNFSM5YCITGBA>
.
You are receiving this because you were mentioned.Message ID:
***@***.***>
|
| @@ -0,0 +1,31 @@ | |||
| export function remainingOrders(timeLeft, orders) { | |||
| let juice = []; | |||
| let time = 0; | |||
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
@Ifycode i am unable to run this code with a forEach loop and forOf loop correctly.
There was a problem hiding this comment.
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
```
There was a problem hiding this comment.
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']);
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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']));
|
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: And function 3 just calls it when it needs the time:
|
Attempt at function 3 on mixed juices Exercise.