Working with Loops - What Is a While Loop, and How Does It Differ from the Do...while Loop?

Tell us what’s happening:

This is not a problem, just an observation from my humble perspective.
In the example on the ‘while loop’ page, we do know how many times the loop will run as it is hard coded, which perhaps does not demonstrate the ‘unknown’ part of while effectively, since it contradicts the topic page that tells us we don’t know how many times it will run.

let counter = 0;
while(counter < 5) {
console.log(counter);
counter++;
}

In my example, the loop could exit any time.

const countries = [
"Afghanistan",
"Albania",
"Algeria",
"Andorra",
"Angola",
"Antigua and Barbuda",
"Argentina",
"Armenia",
"Australia",
"Austria",
"Azerbaijan",
"Bahamas",
"Bahrain",
"Bangladesh",
// Truncated
];

const countryToFind = "Zambia";
let randomCountry = "";

while (randomCountry !== countryToFind) {
  randomCountry = countries[Math.floor(Math.random() * countries.length)];
  console.log(randomCountry);
}

console.log(`${randomCountry} found.`);

Your browser information:

User Agent is: Mozilla/5.0 (X11; Linux x86_64; rv:152.0) Gecko/20100101 Firefox/152.0

Challenge Information:

Working with Loops - What Is a While Loop, and How Does It Differ from the Do…while Loop?

GitHub Link: freeCodeCamp/curriculum/challenges/english/blocks/lecture-working-with-loops/6732c06654ea3442724284fe.md at main · freeCodeCamp/freeCodeCamp · GitHub

I don’t think it is a good idea to put in the lesson an example of a potential infinite loop, or a loop that can go on for a long time.

It is possible to have a really long streak of random countries that are not the country one wants to find, which can cause the browser to crash if one was to test this code.

Isn’t that the situation with any while loop where the number of loops is unknown? The point seems to be that it does loop until the condition is met, which could be a long time as it is unknown.

What could be an alternative that does demonstrate the ‘unknown’ nature of it?

The text from the lesson states that: while loops are useful when you do not know how many times you need to run the block of code. Here is an example of using a while loop:

while (arr[0] % 2) {
arr.shift()
}

for example this, at most will iterate over the whole of arr and empty it (becuase undefined % 2 is NaN so falsy)

Thank you for that. Perhaps the lesson needs updating, so that learners don’t try to create while loops like I did. That said, it did help my learning and understanding of it. Is an array of countries really that bad?

I think this lesson needs several examples, in various contexts.

no, the array of countries is not bad, it’s the randomly searching for one that can cause the loop to be potentially infinite

Okay. I understand that, but at what point/size do we decide that a loop is bad?

What if we were using a computer to count the suns in visible space? Of course, the data is expanding.
We might segment space into squares and and while we haven’t counted all of the suns in that square (discounting things that are not suns) keep counting. When all of the suns have been counted, we move on to an adjacent square. That is a massive count, but we would be in a loop for each square. Would we use something other than loops in that kind of situation?

While (not while) loops seem simple, the application of them requires more than simplicity.

you can work with long running queries, not in the browser tho, the browser just crashes

there are environments and languages that can deal with it tho