What the [edit]?


#1

I'm following along with the "Hint" instructions as much as I can, but still nothing works. I keep getting a msg ("Oops, try again. Are you logging 'I'm looping!' to the console three times?") which is what I think it's supposed to be doing. But I'm flummoxed.

The frustrating thing is that the instructions seem so straight-forward, and I think that I am following along well enough, but actually writing the code never seems to work. I don't know what I'm not getting here.

//Remember to set your condition outside the loop!
var count === 0;

var loop = function(count){
	while(count < 3){
		console.log("I'm looping!");
		var loop = count++;
	}
};

loop();

#2

var count = 0;

var loop = function(){
while(count < 3){
console.log("I'm looping!");
count++;
}
};
loop();

#3

Well at least I was on the right track. Thanks for that. But I do have nagging questions.

  1. var loop = function() - I guess I assumed something HAD to go in the parentheses. That is not the case?

  2. What is the purpose of the second "loop()" at the bottom? I was thrown thinking that had to be filled in somehow.


#4

1.The function is just a container for the loop its really not needed and dosent need any parameters
2. the loop(); part calls the loop function from your first question


#5

Hi,
This is mine and it passed. Yours is not far off it. I know what you are going through as I felt that way too, got annoyed and started to doubt myself. Part of it is that the explanations are sometimes confusing and the hints sometimes inadequate. It's worse on the Python course. There are often a number of ways things can be done but the program only accepts one way which doesn't help. I've gotten through a few Codecademy courses by doing as much as I can, trying different things, then if I'm still stuck I go to the forum and see what others are doing without self-judging. I'm finding that the the forum admin doesn't explain things very clearly so other students are much more useful to me. I've accepted now that each mistake is a learning experience in what is a very complex field. Anyway that's what I think.

var count = 0

var loop = function(){
while(count<=3){
//Your code goes here!
console.log( "I'm looping!");
count++
}
};

loop();


#6

There are actually a few real bugs in @joeirish's code so it's not just about a different version that should be ok as well.

The first problem is that === is a comparison operator but and not an assignment operator (=):

var count === 0;

So this line should already produce an error. The urgency to use a parameter without knowing why then causes a whole bunch of problems. I'd strongly recommend that you review the part about functions or ask about them a bit @jchase0487 already gave you some information but if you need more ask for it as they are really powerful if you know how to use them.

The general idea behind a parameter is that you create a local variable for the function which gets information assigned to it from outside the function.

For example:

    var square = function(x){
       console.log(x*x);
    };
    square(2); --> 4
    square(3); --> 9
    square(4); --> 16

So the value in the () of the function call (e.g. square(2) ) is assigned to the parameter in the () after the function keyword (e.g. x). This let's you call the same function with different input to get different outputs. As you don't use any input you don't need any parameters for the function.

The problem is now that by using count as parameter you "shadow" the global variable count. Meaning even if you had the var count = 0; correct outside of the function by using the paramter count this variable would be unaccessible. Because you created another variable with the same name. As an example you can have a look at Lesson 3 I guess exercise 7 (the one where you just added the var).

Now as you have a parameter but no input the value of count inside of your function is undefined (because the () after loop were empty). So count < 3 means undefined<3 which is false so you're loop wouldn't run anyway. Last but not least is this statement:

var loop = count++;

The count++ is necessary to increase the value of count so that you only run 3 times (in case all the previous errros were fixed). But why did you create another variable that has the same name as the function and is never used anywhere else?

@darkhorse1 I'd really appreciate if you could give constructive critique for example instead of talking about confusing and inadequate hints rather give real examples what you mean and how it could be done better.

Btw your "solution" doesn't work. It would print the text 4 times instead of the required 3 times and a second loop() would not produce any output because the counting variable was declared globally.

A better position for the var count = 0 would be before the loop but inside of the function.


#7

Thanks for the help with the coding but-
A forum is supposed to be a place where people interact as a community provided they stay within forum rules, as we all do.
You made the comment:
"Btw your "solution" doesn't work."

FYI my "solution" passed in the editor. I didn't use the word "solution", I simply presented my code as a code which passed, as we all do to help each other. We often have no choice but to assume a code is correct because it passes. If it doesn't work then it shouldn't have passed. The fault is with the Codecademy course, not with me.


#8

The course interface is definitely a big problem. If you want to test your javascript, there are other options.
Try
http://www.w3schools.com/js/tryit.asp?filename=tryjs_output_write


#9

Thanks very much for that. I'll try it.


#10

There’s also labs repl.it
And also this thing here:
http://www.pythontutor.com/javascript.html#mode=edit
which I haven’t tested thoroughly but which seems to be pretty interesting.


#11

If I see a code without further explanation and a message attached to it being something like "This worked for me", I'd label it a "solution". And I don't like this kind of help. Mainly because it has several problems.

First of all it is not clear that it is visible to the author what the actual problem is and what solved it so that the bug doesn't occur a second time or at least can be identified. Also keep in mind that the exercises are rather short so in the end the codes will all look pretty similar and an "I almost got it" might not always be correct. Also the question "typo or lack of understanding" is an individual one. The second thing is it is not save that it is really a solution and free of bugs. That is of course a risk every comment has but for just code comments it might be worse. Third, all the exercises should be possible if you stick to the instruction both technically and from understanding. And if this is not the case it is better to point that out than give advices on how to "cheat" on the exercise. And as it is about learning your basically cheating on yourself because this may lead to a lack of understanding of the basic concepts.

There might even be more reasons but I guess these are enough for now. I'd rather recommend using code snippets showing the problem and giving hints about the underlying concept. Or at least mark the problems in your solution.

Also there is no reason for posting a solution if there is already a solution in this post, this just makes the topic unnecessarily long and confusing. At this point I'd like to cut this to not make the post any longer. @joeirish was your question answered or do you have any problems left?


#12

Hey all, remember to abide by our community guidelines.

Be Agreeable, Even When You Disagree

You may wish to respond to something by disagreeing with it. That’s fine. But, remember to criticize ideas, not people. Please avoid:

  • Name-calling.
  • Ad hominem attacks.
  • Responding to a post’s tone instead of its actual content.
  • Knee-jerk contradiction.
  • Instead, provide reasoned counter-arguments that improve the conversation.

This site is operated by your friendly local staff and you, the community. This is a Civilized Place for Public Discussion.

  • If You See a Problem, Flag It
  • Always Be Civil
  • Think about the tone you are setting

Please treat this discussion forum with the same respect you would a public park. We, too, are a shared community resource — a place to share skills, knowledge and interests through ongoing conversation.

These are not hard and fast rules, merely aids to the human judgment of our community. Use these guidelines to keep this a clean, well-lighted place for civilized public discourse.


#13

Happy Monday! FWIW, I did re-write the code along the lines darkhorse1 described and it passed. (Thanks for the help, all.)


#14

Thanks so much to help me by this code.


#15

My friend, that is helped a lot.
Thank you, you rock.