6. Solo flight


#1

Please, help me, what is wrong with my code?

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

soloLoop();


#2

Inside the soloLoop function, write a while loop that takes an initial condition that's true. Your loop should log "Looped once!" to the console, then change that initial condition to false.

You wrote myCondition outside of the loop. Here is what you have, and what you should have

// what you have
var myCondition = true
var soloLoop = function() {
  while(myCondition) {
    console.log("Looped once!");
    myCondition = false;
  }
}
soloLoop();

// how it should be
var soloLoop = function() {
  var myCondition = true
  while(myCondition) {
    console.log("Looped once!");
    myCondition = false;
  }
}
soloLoop();

#3

Thank you very much! It works now.


#4

3 posts were split to a new topic: 6. Why else didn't this work?


#5

Hi!

The comment stated that the condition needed to be outside the loop, so aren't we supposed to have "var statement = true" before the function line or does "outside the loop" refer to something else?

my code passed in both the Codecademy Labs as well as the simulator in the tutorial:

//Remember to make your condition true outside the loop!
var statement = true;
var soloLoop = function() {
    while(statement) {
        console.log("Looped once!");
        statement = false;
    }
}; 
soloLoop();

Is it possible that the missing semi-colon in the original made a difference or why would mine pass but not the OPs?

I'm new at javaScript and not trying to be a smartass. I genuinely want to understand. Seems like there's the potential for various solutions and just trying to figure out which ones are the best and why you would choose one over the other. So I have a lot of questions like what the heck is "soloLoop();" for? Rather fuzzy on its function - though I've noticed that the code doesn't function without it, just don't know why. And why would you use a While Loop for a single, non-recurring run or perhaps this just an exercise to test our mettle?

Much appreciation for any clarifications - just pick any! Thank you for bearing with barrage of questions.


#6

First of all @igorcaletacar is correct this is the better way to write it and if you want to see why run your code with

soloLoop()
soloLoop()

at the end and you should see that @igorcaletacar's solution is still working while the one using a global variable fails (just prints one). Still I'd have thought as @tango mentioned that this should work as well. Although it doesn't make much sense as the purpose of a function is to be called more than once. But currently I can't find the error. @vtenity what was the error message? Did it vanished after refreshing the page?

@tango13 The function is just there for ... well ... reasons. The best reason I could think of is to continuously practice functions and according to the number of people struggling with this really easy function "framework" there might be a real reason why it pops up. If this is unknown maybe review Unit3. I'm short of time now so maybe I'll add something to this later.


#7

thank you @haxor789!

what confuses me about the function of soloLoop(); is the empty parameter. it's "calling" the function, which also has an empty parameter, so I don't see the point of either one being empty.

     var statement = true;
     while(statement) { 
         statement = false; 
     }
     console.log("Looped once!");

The above prints out the statement, and repeating console.log 3 times also prints it 3 times. But why would I not choose this method over the others? Is this wrong? probably thinking too hard about it as there are so many things I don't see or know, yet, but that's how I learn, trying to get to the bottom of the "whys".

I think I understand two different ways to crash computer, though. I added " = true" to the while condition and there was a continuous loop of output. I purposely removed "statement = false;" cuz instructions said not to and I needed to see that happen. Understand why removing "false statement" loops but think the adding true to the while condition is a glitch...or not?

Thank you!


#8

Ok first let's establish some terms:
arguments: The values in the () at the function call e.g. console.log("test") here "test" would be an argument.
parameters: The variable names in the () after the function keyword e.g. var square = function(x){... here x is a parameter.

As the connection is done var parameterN = argumentN. Meaning the n-th argument gets assigned to the n-th parameter, it's perfectly valid to call a function without arguments if the corresponding function takes has no parameters. For example because it is not dependent on an input and should just do something like for example Math.random(). So in this case the function is just there to make the while loop reusable.

From what I can see this code runs a completely redundant while loop once and after this prints the "Looped once!" message which of course works because you used a while loop and printed the statement once but is not how it is supposed to do. Maybe run this code here:
http://www.pythontutor.com/javascript.html#
to see it clearer.

The problem with this is that the = operator means assignment not comparison and if you inspect console.log(x = value) you'll see that the expression x = value has a value of well value. So in your case assigning true to your conditional variable has a value of true and while(true) is the classical example of an infinite loop. With false it goes the other way around and not loops even once. Hope this helps.


#9

Thank you @haxor789, that helped alot!

And that link you sent is a really nice tool! Definitely saving it for future use. I've read from either here or elsewhere about the steps that coding takes to execute so it was nice to see it in action.

One thing that I have noticed, though, is when it seems like I'm passing the tutorial but it doesn't seem right somehow, it's nice to have a forum of people to check my work by. Particularly when it just doesn't make sense. Need to have somebody pound some sense into me. Particularly now at the point where all the syntax and terminology of JavaScript haven't quite set in my brain, yet. For example, your last statement about the infinite loop...gonna' have to read it again, possibly after redoing more loop exercises, again. I was dizzy after reading it, sorry to say.

Any suggestions for websites that have JavaScript exercises for practice and just for fun? They're kinda' like puzzles and work my noggin so enjoy doing them, plus the problem-solving also really helps me to better understanding what the heck I'm doing.

Many, many thanks for all your help!!!


#10

codewars, projecteuler there are probably a few quiz sites that you can google but I'd rather recommend that you get some basics and then just start a small scale project one your own you'll most likely run into enough problems really quick :slightly_smiling: And doing the research will help you grasp some things better.


#11

thank you!

just signed up to a Projecteuler Meetup here in Chicago.

promise not to make my head explode...much...:grin:


#12

A post was split to a new topic: 6. What is wrong in my code?


#13