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

learning exercise 9 #3

Open
Danbaba1 opened this issue Jun 1, 2022 · 8 comments
Open

learning exercise 9 #3

Danbaba1 opened this issue Jun 1, 2022 · 8 comments

Comments

@Danbaba1
Copy link

Danbaba1 commented Jun 1, 2022

Exercise:
Mixed Juices

issue:
.I am unable to use while loop and switch statement to run the code.
. Also I am unable to come up with an algorithm to solve the code.

Where:
function 2

@Ifycode
Copy link
Member

Ifycode commented Jun 5, 2022

@Danbaba1 I've seen my error with the switch statement while we were taking a look at the exercise together earlier. It was supposed to be case "small": and not case lime === "small":. I told you it must be one silly mistake I must have been making - and there it is. Also, just like I said using a while or do-while loop doesn't work here.

  • Copy and paste the function below unto your exercism editor, let me know if the test passes or not. Just reply here in this issue, no need to send a mail.
  • I will make a video to explain it. I will make and put the link to the video here in this issue comment tomorrow evening or night.
function limesToCut(numberOfLineWedges, supplyOfWholeLimesArray) {
  let addUpWedgesFromArray = 0;
  let numberOfLimes = 0;

  supplyOfWholeLimesArray.forEach((lime, index) => {
    let amountOfWedgesFromLime = 0;
    switch(lime) {
      case "small":
        amountOfWedgesFromLime = 6;
        break;
      case "medium":
        amountOfWedgesFromLime =  8;
        break;
      case "large":
        amountOfWedgesFromLime = 10;
        break;
    }

    if (addUpWedgesFromArray < numberOfLineWedges) {
      addUpWedgesFromArray += amountOfWedgesFromLime;
      numberOfLimes = index + 1;
    }
  });

  return numberOfLimes;

}

limesToCut(25, ['small', 'small', 'medium', 'large', 'small']);

@Danbaba1
Copy link
Author

Danbaba1 commented Jun 6, 2022

@Ifycode it worked in the exercism editor.
but is there a way i can use a while loop and switch statement

@Danbaba1
Copy link
Author

Danbaba1 commented Jun 6, 2022

@Ifycode what about the third function in exercise 9

@Ifycode
Copy link
Member

Ifycode commented Jun 6, 2022

Well, there are two things I will say concerning this.

  • Remember I said that I'm thinking if do-while or while loop is needed at all for this function. Probably it's the way I think about code. But I didn't see it working from start.
  • Also, It's not compulsory that you use everything in the lessons in all the functions. Especially if using something will make it overly complex. That is what I feel about using those loops in this case.

But I could try again to see if a do-while or while loop would work. But definitely not with forEach, because it gives a wrong answer.

@Ifycode it worked in the exercism editor.
but is there a way i can use a while loop and switch statement

@Ifycode
Copy link
Member

Ifycode commented Jun 6, 2022

@Ifycode what about the third function in exercise 9

Ok, I was thinking that you said it's the 2nd function you are having problems with. So I didn't attempt that.

So try it out if you haven't. If you don't get it by tomorrow morning or noon, do this.

  • Create a new branch from main
  • In the new branch, create a file called 9.js, then copy and paste everything you have on your exercism editor in there.
  • Send a pull request (no need to create another issue. Just link the pull request to this issue since it's still about learning exercise 9).

@Danbaba1
Copy link
Author

i was able to use a while loop with the second function on mixed juices exercise.

function limesToCut(wedgesNeeded,limes){
let limeCut = 0;
let wedgeCut = 0;
let index = 0;
let numLimes = 0;
while(index < limes.length){
switch(limes[index]){
case 'small':
wedgeCut = 6;
break;
case 'medium':
wedgeCut = 8;
break;
case 'large':
wedgeCut = 10;
break;
}
if(limeCut < wedgesNeeded){
limeCut += wedgeCut;
numLimes += 1;
}
index += 1;
}
return numLimes;
}
console.log(limesToCut(0,['small','small','large','medium','small']));

@Ifycode
Copy link
Member

Ifycode commented Jun 13, 2022

i was able to use a while loop with the second function on mixed juices exercise.

function limesToCut(wedgesNeeded,limes){ let limeCut = 0; let wedgeCut = 0; let index = 0; let numLimes = 0; while(index < limes.length){ switch(limes[index]){ case 'small': wedgeCut = 6; break; case 'medium': wedgeCut = 8; break; case 'large': wedgeCut = 10; break; } if(limeCut < wedgesNeeded){ limeCut += wedgeCut; numLimes += 1; } index += 1; } return numLimes; } console.log(limesToCut(0,['small','small','large','medium','small']));

Screenshot 2022-06-13 at 8 15 38 PM

Nice work, well done @Danbaba1
I have written a reply to the comment on the PR for function 3. Pasting your code will give me a better idea of what you are struggling with.

@Danbaba1
Copy link
Author

@Ifycode thank you so much

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

No branches or pull requests

2 participants