NaN error when trying to include math output in a console.log


#1



This is what I get: You rolled a "NaN! You actually hit him!
Instead of "NaN!, it should say, You rolled a 20!

Can someone tell me why I'm getting NaN? I read elsewhere that it could be because I didn't initialize the vars, but I tried that and it didn't work. Thanks!

Sorry in advance for the cheesetastic nature of this.


Replace this line with your code. 
var user = prompt("You've encountered Demogorgon... Oh crap, he's seen you. Whatever are you going to do? (Soil myself, Run, Attack)").toLowerCase();
var rollStamina = 1;
var playerAttack = 1;
var demogorgonAC = 18;

switch (user) {
    case 'soil myself':
        console.log("Can't say I blame you.");
        var diapers = prompt("Are you wearing diapers?").toLowerCase();
        var char = prompt("Do you want to roll Charisma? See if he'll spare your pathetic hide?");
        if(diapers && char === "y") {
            console.log("Your huggies catch <i>most</i> of the splatter, and you pass your charisma roll. He spares life, chuckling evilly as he walks away. You get the feeling you'll meet again someday...");
        } else {
            console.log("Well, can't say I blame you. Unfortunately, human feces is more of a Demogorgon attractant rather than a deterrent. He devours you, starting at the tail end... It's not pleasant.");
        }
        break;
    case 'run':
        var bolt = prompt("Are you Usain Bolt? (Y or N)").toLowerCase();
        var rollStamina = Math.floor(Math.random()*20+1);
        if(bolt === "y" || rollStamina >= 18) {
            console.log("Holy crap. Your stamina roll was ") + rollStamina + ("! You actually got away! Wasn't expecting that!");
        } else {
            console.log("Oh the conceit, to think you can outrun the Lord of All that Swims in Darkness! You try to run, but Demogorgon easily outpaces you. He nudges you into a briar patch and watches you flail in agony for a few moments before he devours you.");
        }
        break;
    case 'attack':
        var playerAttack = (Math.floor(Math.random()*20+1) +10);
        console.log("You draw your vorpel sword and charge!");
        if(playerAttack >= demogorgonAC) {
            console.log("You rolled a")+playerAttack+("! You actually hit him! You hit Demogorgon! He's never felt pain before, and it stuns him long enough for you to escape!");
        } else {
            console.log("You flail mightily, but rather than injuring him, you only succeed in proving that your are unworthy of the blade in your hands. He casually brushes off your attack, then grabs you around your middle, laughing as he slowly squeezes the life from your ribs. The last thing you hear is the wet tearing of one of your arms being torn from your socket.");
        }
        break;
    default:
        console.log("Ooh... Poor choice... Demogorgon casually plucks your head from                      your shoulders and eats your brains for a snack. Tasty!")
};


#2

i think the problem is that you are trying to add a number to a string? i'm not 100% confident, but your code looks good elsewise from what i can see, so i think you need to concatenate the strings with toString(playerAttack) and toString(rollStamina), tell me if this works
and i don't play dungeons and dragons, but this is one of the funniest, most creative programs i've seen, so i commend you
I just realized that I've helped you before, so hello again lol :))

edit: i edited casting the integers as strings because i was trying to use a python function, but @825orion had the right one.


#3

Haha! Excellent story!

You're probably getting NaN on the playerAttack variable. You are concatenating the string to an integer, which in most cases won't work. Here's an updated method using toString()

New line:

var playerAttack = toString(Math.floor(Math.random()*20+1)+10);

Documentation regarding toString() can be found here.

Let me know if you need any more help.


#4

hey thanks for the help, I figured that it was because of the integer, but i had no idea how to cast it as a string in JS.


#5

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