Exercise 7.8 Passing exercise but nothing is being logged


#1


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


When I run the code below I get a success message but the console only shows "logged". I've looked through the code many times but can't find a reason as to why neither function is being logged. Did I over look something?


var forExample = function() {
    for(i=0; i>=4; i++){
        console.log("Current value of i is: " + i + " there will be " + 4-i + " more loops in this for loop example.");
    };
};

var whileExample = function (myCondition) {
    while(myCondition < .35) {
       console.log("Current value of myCondition: " + myCondition + " I wonder how many loops we'll need before it is greater than .35");
        myCondition = Math.random();
    };
};

forExample();
whileExample(Math.random());
console.log("logged");


#2

If you can't find a reason why it isn't logged, can you find a reason why it would be? Your code is a set of instructions, do those instructions amount to saying to do any other printing?

Let's run it 10 times:

$ for i in {1..10}; do node bitcoder18908.js; done
Current value of myCondition: 0.22937763299798353 I wonder how many loops we'll need before it is greater than .35
logged
Current value of myCondition: 0.05077272027978963 I wonder how many loops we'll need before it is greater than .35
Current value of myCondition: 0.21340941634222488 I wonder how many loops we'll need before it is greater than .35
logged
logged
logged
Current value of myCondition: 0.2651245053968514 I wonder how many loops we'll need before it is greater than .35
logged
logged
logged
logged
Current value of myCondition: 0.17520979168664375 I wonder how many loops we'll need before it is greater than .35
Current value of myCondition: 0.04925947876275316 I wonder how many loops we'll need before it is greater than .35
logged
logged

#3

To my understanding console.log() should print the contents of it's parenthesis to the console each time that bit of code is run. So each time either for or while loops are ran the contents of each console.log should be printed right? At the very least that is how it appears to work in my previous exercises.

For example when I ran the following code in 7.6:

var soloLoop = function(loopOnce){
  while(loopOnce){
      console.log("Looped once!");
      loopOnce = false;
  };
};
soloLoop(1);

console.log("I'm tired of using while loops that end when expect them to. I'll make my own even if the lesson isn't cleared.");

var randomLoops = function(randomCondition) {
    while(randomCondition < .85) {
        console.log("The current value for randomCondition is " + randomCondition + " that is less than .85, running the code again.");
        randomCondition = Math.random();
    };
};
randomLoops(Math.random());

The following was returned:

Looped once!
I'm tired of using while loops that end when expect them to. I'll make my own even if the lesson isn't cleared.
The current value for randomCondition is 0.7538330982239323 that is less than .85, running the code again.
The current value for randomCondition is 0.37157737165549176 that is less than .85, running the code again.
The current value for randomCondition is 0.3825677402065868 that is less than .85, running the code again.
The current value for randomCondition is 0.8188529879640116 that is less than .85, running the code again.

Is my understanding of how console.log() works incorrect?


#4

If calling console.log has the effect of printing characters to console, and characters aren't printed to console, then the conclusion to draw is that console.log wasn't called, right? How many iterations is your loop doing? Does program control reach the loop at all?


#5

BTW, while and for-statements do not end in semi-colon, neither do if-statements or function statements. Assignment statements do.

// yes:
var f = function() {};

// no:
function f() {};

Pretty harmless for the most part, you just create empty statements. Look up the statement you're wondering about if you're not sure.


#6

I see my mistakes now, I've changed the code to the following:

console.log("Does program control reach here?"); // yes

var forExample = function() {
    console.log("Does program control reach here?"); // yes
    for(var i = 0; i <= 4; i++) { // I see I had the wrong comparitor so the loop wouldn't run
        console.log("Current value of i is: " + i + " there will be " + (4-i) + " more loops in this for loop example.");
    }
}
forExample();

var whileExample = function (myCondition) {
    console.log("Does program control reach here?"); // yes
    while(myCondition < .65) { // raising this value just for better odds of rerun
       console.log("Current value of myCondition: " + myCondition + " I wonder how many loops we'll need before it is greater than .65")
        myCondition = Math.random()
        // The original Math.random() value passed into my argument when calling the function must of been below .35 in my previous attempts
    }
}
whileExample(Math.random());

console.log("logged");

Thank you for the help I'll have to keep in mind whether or not the program control can reach the loops if this happens again in the future.

Also I'm not 100% sure I understand what you mean by:

function f() {};

Thank you for the help :slight_smile:


#7

That's a function statement. Its syntax doesn't have a semi-colon in it. So that's a function followed by an empty statement.

All you really need to keep in mind is to check if the code is doing as you expect. Find out rather than guess!


#8

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