Ifycode-support / Danbaba1-exercism-issues

0 stars 1 forks source link

learning exercise 9 #3

Open Danbaba1 opened 2 years ago

Danbaba1 commented 2 years ago

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 commented 2 years ago

@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.

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 commented 2 years ago

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

Danbaba1 commented 2 years ago

@Ifycode what about the third function in exercise 9

Ifycode commented 2 years ago

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

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 commented 2 years ago

@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.

Danbaba1 commented 2 years ago

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 commented 2 years ago

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 commented 2 years ago

@Ifycode thank you so much