FAQ: Loops - Do...While Statements

Tag me in a topic where you posted a question and we can give it a go.

They ask us to do this "We have a sweet tooth, so we want to add at least one cup of sugar to the batter even if the value of cupsOfSugarNeeded is 0 "
So, for me the code should be this :

let cupsOfSugarNeeded = 2;

let cupsAdded = 0;

do {
  cupsAdded = cupsAdded + cupsOfSugarNeeded;
  cupsAdded ++ ;
}
  while (cupsAdded < cupsOfSugarNeeded) ;
  
console.log(cupsAdded);

// prints 3, so one more cup of sugar than the recipe.

Is it correct ?

1 is one more than zero, and 3 is one more than two, so it adds up.

1 Like

Thanks @mtf.
I known this was correct because I ran it.

But Codecademy gives this answer to this exercise :

// Write your code below
let cupsOfSugarNeeded = 2;
let cupsAdded = 0;

do {
 cupsAdded++
} while (cupsAdded < cupsOfSugarNeeded);

console.log(cupsAdded);
// 2

And this code doesn’t answer to what we have to do.

That is the only line needed. The difference between it and yours is that the loop runs three times whereas yours only runs once.

1 Like