FAQ: Loops - Review

This community-built FAQ covers the “Review” exercise from the lesson “Loops”.

Paths and Courses
This exercise can be found in the following Codecademy content:

Web Development

Introduction To JavaScript

FAQs on the exercise Review

There are currently no frequently asked questions associated with this exercise – that’s where you come in! You can contribute to this section by offering your own questions, answers, or clarifications on this exercise. Ask or answer a question by clicking reply (reply) below.

If you’ve had an “aha” moment about the concepts, formatting, syntax, or anything else with this exercise, consider sharing those insights! Teaching others and answering their questions is one of the best ways to learn and stay sharp.

Join the Discussion. Help a fellow learner on their journey.

Ask or answer a question about this exercise by clicking reply (reply) below!

Agree with a comment or answer? Like (like) to up-vote the contribution!

Need broader help or resources? Head here.

Looking for motivation to keep learning? Join our wider discussions.

Learn more about how to use this guide.

Found a bug? Report it!

Have a question about your account or billing? Reach out to our customer support team!

None of the above? Find out where to ask other questions here!

I’m trying to practice applying each type of loop (i.e. for, while, do…while) to the existing list in the review section for Loops

Array ==> let groceryList = [‘orange juice’, ‘bananas’, ‘coffee beans’, ‘brown rice’, ‘pasta’, ‘coconut oil’, ‘plantains’];

I want the code to loop through the array and log each element as long as that element doesn’t equal ‘pasta’ and I’m trying to accomplish this by using a do…while loop but for some reason it keeps running into an infinite loop. What am I doing wrong?

My code

let j = 0;
let grocery;

do {
  grocery = groceryList[j];
  j++;
} while (groceryList[j] != 'pasta');

console.log(grocery);

I am confused.

I have been following each instruction and following all the cheatsheets and online tips. I keep getting the code wrong. I don’t know if it’s the way everything is worded or what.

Does anyone have any suggestions on where I could find videoes or helpful hints/tips to help me work through these?

I am having an extremely difficult time understanding this- not just loops, but functions and whatnot. I am going CRAZY and can’t get ANY of the code right. I must be dense or something but feeling HOPELESS

If you don’t mind my asking, what attracted you to programming? Was it the challenge?

I dont mind at all. I thought that coding is a language that one must know given the time we live in where technology is an integral part of our daily lives. Then as I started considering taking a class, I got a bunch of ideas of what I want to build and now, I am determined to do it. But its extremely difficult- HTML and CSS was a walk in the park, as soon as JavaScript came into the picture, it was all over. I understand the concepts while I am learning them, but then I can’t recall them, and with projects, I can’t seem to think outside the box like I have to. And dont get me started on trying to remember where semi colons, curly braces, and so on go. :rofl:

What you are experiencing are called, ‘growing pains’. Everyone experiences them to one degree or another. A long list of learners hit the same wall you did once JS came into the picture. Perfectly normal and expected. Don’t give up.

All languages have a set syntax, JS being no different. It actually adopted what was already a well established syntax common to Java, C, and other similar class based language, of which JavaScript (ECMAScript) is not one.

It doesn’t take long for curly braces and semi-colons to become second nature. Think in terms of ‘template’.

console.log("This is a statement.");

const statement = "This is a statement.";

const a = 42;

All of the above have the same thing in common: they are statements. They all end with a semi-colon which tells the parser, ‘end of statement’ and it stops reading-in the line at that point and begins to interpret it.

Curly braces define objects, whether as ‘associative array’ like objects,

b = {
    x: 3,
    y: 4,
    z: 5
}

where each item in the above ‘array’ is a key-value pair, and are separated by commas (the curly braces act as delimiters and define the boundaries of the object); or, they are used to define the body or block of a code segment in one of several constructs used in JS.

if (condition) {
    // action statement;
}

The above is a simple if statement. Note that there is no semi-colon after the curly brace, only the line(s) contained inside the block. Notice the parentheses around the condition? The condition may be any expression (something that resolves to a value) and when we see parens we should not expect to see a statement between them, only ever expressions.

That brings us to functions, which like the above have both parens and curly braces in their syntax.

 const foo = function (param) {
     // code body statements;
     return param;
}

Above, param can be one or more comma separated variable names (parameters) that will receive a value from the function call, so they are still expressions, as such.

console.log(foo("This is the argument."));

// This is the argument.

Notice how we passed in a value with our call to foo() and it responded by returning it, whereupon it got logged out to the console (the display).

You’re be seeing a lot of these ‘templates’, or patterns in your travels, which is how they will become second nature once the full gravity of their use is apparent. Soon enough you can put the syntax roller coaster behind you and begin to dive into the logic and algorithms of real programming.

Bottom line, expect set backs and confusion. This is perfectly normal and it will get easy with time and continued effort. As long as you are confused, you are learning. Just don’t give up.

3 Likes

I’m in the same boat as you my man. I actually was just talking about this the other day; how frustrated I get taking the time to learn the concepts in depth and failing to recall them and being unable to write a block of functioning code without needing to google or check documentation… how going from HTML & CSS to JS is an entirely different beast. But one thing I know for sure, is if you refuse to give up, and to keep coding you’ll have no choice but to become great.

2 Likes

Agreed 100%. You aren’t growing if you are comfortable right?

1 Like

One thing I’m having trouble with is why the iterator variable in a loop is logged as originally defined, even though the for statement modifies the iterator variable (seemingly) before the code block (containing the console.log statement) runs.

For example, why does this log 0 first, when the for statement includes “counter++” before we get to the “console.log” statement. To me it intuitively seems as though 1 should be added to 0 before the value is logged. Is there any logic behind this that can help me understand it better?

for (let counter = 0; counter < 4; counter++) { console.log(counter); }

The “while” expression makes more sense to me, because you increase the variable after the console.log statement (notably, as part of the code block).

let counterTwo = 1; while (counterTwo < 4) { console.log(counterTwo); counterTwo++; }

I would appreciate any insights/help, thanks!

The three parts in the parameter are not executed in the order they appear. First the iteration variable is defined (declared and given an initial value). Second the code inside the block is executed. Third the variable state is tested against the condition and if the condition is still met, then the increment takes place and the block code is repeated.

i = 0
console.log(i)  //  0
i < 4           // true
i++
console.log(i)  //  1
i < 4           //  true
i++
console.log(i)  //  2
i < 4           //  true
i++
console.log(i)  //  3
i < 4           //  true
i++
console.log(i)  //  4
i < 4           //  false  =>  loop terminated
1 Like

Thank you (and thanks for responding so quickly). That’s really helpful.

1 Like

Imagine if there is no pasta in groceryList, what will the program do or output to the console?

The answer to this question depends on the type of problem you are facing, is it about how to write basic syntaxes like loops, conditionals etc.? Is it about deducing a logic based on the given scenario? Or is it about integrating the logic with code within a loop or other data structures?

I would suggest regardless of what problems you are facing with your code, always have a debugger at hand so that you can trace at which line of code or structure the error lies upon. Also, console.log is your best friend !!! Always display the input and each intermediate variables to the screen so that you know whether your logical thinking being put into code is on the right track! Happy coding!

This is how your code should be

let groceryList = [‘orange juice’, ‘bananas’, ‘coffee beans’, ‘brown rice’, ‘pasta’, ‘coconut oil’, ‘plantains’];
let j = 0;
let grocery;

do {
grocery = groceryList[j];
console.log(grocery)
j++;
} while (groceryList[j] != ‘pasta’);