10/11 Need help understanding why my syntax is incorrect


#1

var getToDaChoppa = function(x){
// Write your do/while loop here!
do { console.log("Throw down your weapons!");
};
while (x>3) { console.log("looping!");
x--};
getToDaChoppa(5);


#2

try this

var getToDaChoppa = function(x){
// Write your do/while loop here!
i = 0;
do { 
    console.log("Throw down your weapons!");
    i++;
    
}while (i<x);
};
getToDaChoppa(5);

this is the correct syntax for do/while loop

//correct syntax for the do/while loop
do{
    //statements
    //termination statement goes here too
}while(conditon);

#3

Rydan,

Thank you for responding so quickly. I guess I wasn't considering that you don't have to put code to be executed after the while logic.

Can you walk me through a bit of your logic for putting together the "i" statements? I guess the function logic is still a little fuzzy to me.


#4

you can also do this if it makes sense

var getToDaChoppa = function(x){
// Write your do/while loop here!
do { 
    console.log("Throw down your weapons!");
    // we have to keep decrementing x or the loop will run forever
    x--;
    
}while (x>0);
};
getToDaChoppa(5);
//new line
console.log("\n");
getToDaChoppa(-5);

#5

in this case do you have to increment i inside the do code clock?
What would happen if you increment i just after the do code block?


#6

Yes you would have increment/decrement in the do block.
if you do it outside the block you will get an error because the x we are using here is only l**ocal to the function we are defining(i.e. the x variable here only exists when we call the method)**


#7

if you incremented x just after or just before the do code block but inside the function?
In lesson 9

var loopcondition = false

is defined before the do statement.


#8

it helped me understand it better thank you!