Theory Question? Deviating from the tutorial~


#1



https://www.codecademy.com/courses/javascript-beginner-en-mrTNH-6VIZ9/0/5?curriculum_id=506324b3a7dffd00020bf661#


Hello all. I am on the above exercise, and got to the part where we are to repeat the

var youHit = Math.floor(Math.random() * 2)

down in the "else" domain after determining if the damage dealt was enough to slay the dragon or not.

However, I thought it was kind of dumb that we should have to repeat the whole same variable with the same definition. IMO, it'd be nice to call it as a function. So, I created the function below, and called it in the else domain as described.

However, when I test this out, I cannot die. No matter how many times (at least 30+) I tested the code, the player simply hit enough times to slay the dragon, without any sign that the player ever missed.

I am not sure why this broke the randomness/loseability of this dragon slaying game. I'm sure there's some sort of nuance or something that I am not familiar with that is causing my issues.

Is it possible to use a function instead of a simple variable as I've attempted, without convoluting the current code into something else more complicated than it needs to be?

Thank you once more for your service in helping me understand and learn Javascript here! :smiley:


var slaying = true;
var youHit = function () 
    { (Math.floor(Math.random() * 2)); }
var damageThisRound = Math.floor(Math.random()*5 + 1);
var totalDamage = 0;

while (slaying) 
    {
            if (youHit) 
                {
                    console.log("Congratulations, you hit the dragon!");
                    totalDamage += damageThisRound;
                    
                    if (totalDamage >= 4) 
                        {
                            console.log("You slew the fierce dragon!");
                            var slaying = false;
                        }
                    else 
                        {
                            youHit();
                        }
                            
                            
                    
                
                }
            else 
                {
                    console.log("You have become a tasty dragon snack!");
                    var slaying = false;
                }
                
             
                
    }


#2

I believe the youHit function generates a random number that's either 0, 1 or 2 (don't quote me on that, I hate chance functions). And in the if-statement there is no check for the value, only if it contains one.
For example try to change the if to only execute the true if youHit is 1 or smaller.

Play around for a bit with the values within the youHit function and print it in the console.log(); to see what values come out.


#3

You're already calling a function, making a function that just calls another function.. I'm not a fan.

You're not testing the result of the function, you're treating the function itself as a boolean.

You should also work consistency in your indentation (and less blank lines) I usually go with this style:

if (condition) {
    blah;
}

It gets a whole lot easier to tell what happens when if you're consistent with formatting


#4

Thanks for the stylistic recommendations, I just simplified my formatting to fit that more condensed form. I can understand how my layout was probably a bit too spacey/chunky for a well-practised coder to understand intuitively! I guess I was doing it the way I did above, so I could better visualise how each group of code was organised and related to the other. Now I have:

var slaying = true;
var youHit = function ()
{return (Math.floor(Math.random() * 2));
}
console.log(youHit());

var damageThisRound = Math.floor(Math.random()*5 + 1);
var totalDamage = 0;

while (slaying) {
if (youHit() === 1) {
console.log("Congratulations, you hit the dragon!");
totalDamage += damageThisRound;

        if (totalDamage >= 4) {
            console.log("You slew the fierce dragon!");
            var slaying = false;
        }
        else {
            youHit();
        }
}
else {
    console.log("You have become a tasty dragon snack!");
    var slaying = false;
}

}

and it is quite attractive layout.

However, I'm a bit confused still on your answer. You say I am already calling a function. Which function would that be? Would it be the Math.random() function you mean? That would be the only non-"youHit" function present. I see in the tutorial's recommendations, that I would be reassigning the youHit variable to the same thing [Math.floor(Math.random()*2)] beforehand. I was simply trying to make it a function so that I wouldn't have to re-write

Math.floor(Math.random() * 2)

each time. I'm guessing you think it's kind of lazy of me :sweat_smile: .

Using the code above, I was able to get the value of the youHit to put out its either 0 or 1 value. However... when I run the code... even if the code shows youHit = 1 after the Math.floor rounding down, it says the dragon killed me. If I get youHit to give out 0, it still says that.

In another instance I get 1, but this time makes sense because it say's I hit before being killed by the dragon.

So, I'm not sure what's causing these "event" confusions. It's kind of fun to play with, at least.

Sorry if I'm being stubborn. I just want to understand what I'm doing that doesn't make this function ideally. :stuck_out_tongue:

Thanks again!


#5

Well, the Math.random() generates a random number between 0 and 1 (decimals). Then, the Math.floor() rounds DOWN the result to the nearest integer. Thus the following results would come from the youHit function/variable in the following scenarios:

// 1. Math random = 0.1
// 0.1 * 2 = 0.2
// Math.floor = 0

// 2. Math random = 0.4
// 0.4 * 2 = 0.8
// Math.floor = 0

// 3. Math random = 0.6
// 0.6 * 2 = 1.2
// Math.floor = 1

// 4. Math random = 0.9
// 0.9 * 2 = 1.8
// Math.floor = 1

So, the results will always be either 0 or 1 in this case, never 2 as far as I can understand. So, I did change the code around a bit, to check for the value (as far as I'm able to using the same if/else scenario that the exercise is supposed to be testing). If you read my reply to ionatan, you can see an updated version of my code and some of the errant/interesting behaviour I'm witnessing.


#6

The value of youHit() you print to the console isn't the same value as you use in your if-statement. It isn't stored, but generated everytime you call the function.


#7

Oh! I see, so the first value, the 0 or the 1 is one generated value, but then when I check if youHit() === 1, I'm checking a completely different generated value of youHit, not the one seen above. That would definitely explain the disagreement.

Wait. I feel dumb. Instead of making youHit a function, could I simply call youHit as a variable later on? Below, I show what I mean.

if (totalDamage >= 4) {
console.log("You slew the fierce dragon!");
var slaying = false;
}
else {
youHit;
}

Or would this involve just re-using the value that the function generated earlier rather than generating a new value as desired? I think this is what's happening because I've not yet been able to receive a "hit the first time but missed the second and died" event.


#8

I think I've finally figured out what I had to do, such that the programme isn't missing any of the possible events. I just wanted to post the coding that I've made in case someone else is curious and finds this thread. I wanted to post some resolution. Let me know if I'm mistaken, but I think this is what I was after.

var slaying = true;
var youHit = 0;
stab();
function stab() {
youHit = (Math.floor(Math.random() * 2));
}

var damageThisRound = Math.floor(Math.random()*5 + 1);
var totalDamage = 0;

while (slaying) {
if (youHit === 1) {
console.log("Congratulations, you hit the dragon!");
totalDamage += damageThisRound;

        if (totalDamage >= 4) {
            console.log("You slew the fierce dragon!");
            var slaying = false;
        }
        else {
            stab();
        }
}
else {
    console.log("You have become a tasty dragon snack!");
    var slaying = false;
}

}

// 1. Math random = 0.1
// 0.1 * 2 = 0.2
// Math.floor = 0

// 2. Math random = 0.4
// 0.4 * 2 = 0.8
// Math.floor = 0

// 3. Math random = 0.6
// 0.6 * 2 = 1.2
// Math.floor = 1

// 4. Math random = 0.9
// 0.9 * 2 = 1.8
// Math.floor = 1


#9

var slaying = true;
var youHit = 0;
stab();
function stab() {
youHit = (Math.floor(Math.random() * 2));      <- watch out!
}

var damageThisRound = Math.floor(Math.random()*5 + 1);
var totalDamage = 0;

while (slaying) {
    if (youHit === 1) {
        console.log("Congratulations, you hit the dragon!");
        totalDamage += damageThisRound;

        if (totalDamage >= 4) {
            console.log("You slew the fierce dragon!");
            var slaying = false;
        }
        else {
            stab();
        }
    }
    else {
        console.log("You have become a tasty dragon snack!");
        var slaying = false;
    }
}

This is looking pretty good yes, but watch out for local and global variables. It is not necessarily a problem here, but it is something you need to keep in mind for the future.

Great work!


#10

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