My code works, but I can't see it in the console - Practice makes perfect


#1

https://www.codecademy.com/en/courses/javascript-beginner-en-ASGIv/1/1?curriculum_id=506324b3a7dffd00020bf661#

So this is the code I used for the linked exercises. It works, however I can't see it logged into the console. It's like the program can see it, yet it doesn't appear in the little box. The box is empty instead of letting me see "I'm looping" etc.

I've tried doing:
console.log(loop * 3)
console.log(loop("I'm looping))
return(loop *3)
but all of those bring an error message

What can I do to see my code? Or why doesn't adding console.log into it do anything?

Edited for clarification.


var count = 0;

var loop = function(word){
while(count){
 loop *3
count = 1

}
};

loop("I'm looping");


#2

The above line does nothing and may be removed.

Since count is initialized to 0, this loop will never run. It's just as well it didn't, or it would be an infinite loop.

With count set is as,

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

Note that we are changing count inside the loop so that it will eventually fail the condition, causing the loop to terminate. This is very important.


#3

The code you gave me also works, but that's not really what I'm asking for.

My problem is that the lines of code I created also work, I just can't see them in action.
The code I wrote allows me to pass the assignment and move on to the next chapter, however it doesn't appear in the console (it's not an infinite loop or anything). It seems that only the program can register that "I'm looping" appears 3 times.

Could you help me modify the code I created so that I can see it in the console? Or maybe explain why adding console.log into it won't work?

Thank you in any case.


#4

Your code may pass the SCT, but it will not work. That's why there is no output. The loop never runs. If it does, the way it is written, it will be inifinite.

It is a poor practice to use variables in your function with the same name as the function. As stated earlier, loop * 3 should be removed.

var count = 0;
var loop = function (phrase) {
    while (count++ < 3) {
        console.log(phrase);
    }
};

loop("I'm looping!");
I'm looping!
I'm looping!
I'm looping!

#5

Ah all right, I understand now.
And I should've given 'loop' another name within the function like var loop = random, instead of using loop (in general, not for this code).
Okie dokie, thank you.


#6

This particular function only needed two variables. count as a control, and phrase as an input parameter. If you did need another variable, then it should have a name that is,

a. not a reserved word;
b. not already the name of the function; and,
c. that describes its purpose or role in the function.

and that does not have a value that is a reserved word. random is a reserved word, or very nearly one. Using it in our code could result in readability problems and confusion.

Anyway, good that you are making headway. Happy coding!


#7

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.