0

I was learning javascript on Codecademy. Then all of a sudden I encountered the error "Oops, try again. Make sure you print a message onto the console!"

 var slaying = true
 var youHit = Math.floor(Math.random()* 2)
 var damageThisRound = Math.floor(Math.random()*5 + 1)
 var totalDamage = 0
 var dragonSlaying = function() {
     while(slaying){   
         if(youHit){   
             console.log("You hit that bastard");
             totalDamage += damageThisRound;
             if(totalDamage >= 4){
                 console.log("The dragon has been slain");
                 slaying = false;
             } else {
                 youHit = Math.floor(Math.random() * 2);
             }
         } else {
             console.log("You have been defeated; you missed the slimy thing! Maybe next time.");   
             slaying = false;
         }
    }       
    slaying = false;
}
Ian Zhang
  • 11
  • 8
  • 3
    I wish codecademy taught code *indenting* earlier in their program... – random_user_name Feb 13 '16 at 01:35
  • The reason that you are getting that warning from *codecademy* (not from javascript) is because your function `dragonSlaying` is not being **called** anywhere, therefore when you run this, there *is* not output to the console. Add this at the end: `dragonSlaying()` to call the function, and it will do what they want. – random_user_name Feb 13 '16 at 01:39
  • Also, you might want to practice being consistent with some things: notice that you end some lines with a semicolon `;` (this is good), and some lines you don't? Pick a style - but I recommend the style where each command ends with a semicolon (you wouldn't put a semicolon after the `}` in this code, for example) – random_user_name Feb 13 '16 at 01:41
  • Last one, then I'll leave you alone: I edited your question to say "javascript" instead of "java". It's a common mistake, but see this question: http://stackoverflow.com/questions/245062/whats-the-difference-between-javascript-and-java - and, my favorite answer of all time to this question: http://stackoverflow.com/a/245073/870729 – random_user_name Feb 13 '16 at 01:43
  • what are semi colons even for? – user5921073 Feb 13 '16 at 01:43
  • Too much to answer - but this question / answer tells you more about semi colons: http://stackoverflow.com/questions/444080/do-you-recommend-using-semicolons-after-every-statement-in-javascript – random_user_name Feb 13 '16 at 01:44
  • @cale_b To be far... It seems he doesn't use semicolons only for declarations, so its still consistent, but a horrible code line. Also, OP, you should be more consistent about how you use white space for your operations. You've done `x* 2)` and `y*2` and `z * 2`. I recommend the latter, its easier to read. I've worked with too much code that was difficult to read because you couldn't tell where one term ended and the other began. And as another rule of them, I always like to leave a blank line before every new block (while-loop, if-statement, etc) – AustinWBryan Feb 13 '16 at 02:08
  • Just a note: you didn't actually *ask* a question, you just implied it. For future SO questions it would be better if you took the time to explain the situation a bit more and actually spell out your question. – Andrew Feb 13 '16 at 02:41

2 Answers2

2

So here's what I found:

  1. It's a good habit to terminate each statement with a semicolon, so I put ; at the end of each variable.
  2. There is a function expression var dragonSlaying = function() {}; which you didn't call at the end of your code - this is the reason why console.log didn't print a message onto the console - so you should add dragonSlaying(); at the end of your code.
  3. Actually, there is no need for the function at all, you could just omit var dragonSlaying = function() {}; and the code will work perfectly.

Here's the corrected code:

var slaying = true; 
var youHit = Math.floor(Math.random()* 2);
var damageThisRound = Math.floor(Math.random()*5 + 1);
var totalDamage = 0;
while(slaying){   
    if(youHit){   
        console.log("You hit that bastard");
        totalDamage += damageThisRound;
            if(totalDamage >= 4){
                console.log("The dragon has been slain");
                slaying = false;
            } else {
            youHit = Math.floor(Math.random() * 2);
            }
    } else {
        console.log("You have been defeated; you missed the slimy thing! Maybe next time.");   
        slaying = false;
    }
}       

Good luck with the rest of the course!

goformarty
  • 33
  • 6
0

You will have to realize that Codecademy wants you to do things in a specific manner, although the directions can be vague at times.

A working code, sometimes doesnt even go through to the next lesson because of this. Make sure to follow their directions completely and also be sure to watch out for the last step which sometimes includes calling the function.

ps semicolons at the end of code, think of it as concluding a statement or command. if you dont conclude with semicolons the code just runs on to the next lines and then it just doesnt work anymore.