2

I am trying to subtract the value of object1's objectProperty from object2's object property and I keep getting NaN on the console. Here is example code:

    object1.objectProperty - object2.object2Property

If this isn't enough to go off, I can post the full code from my project. If there is another way to do this or some kind of function that can help, please let me know.

edit: Here is the code..

    var myPokemon = {
    health: 25,
    defense: 5,
    attack: 10,
    speed: 5
};

var moves = {
    Scratch: 5,
    Bite: 5,
    Slap: 5,
    Growl: 1
};


var computerPokemon = {
    health: 20,
    defense: 5,
    attack: 10, 
    speed: 7
};

function calcDamage(firstPokemon, secondPokemon, move) {
    if(move == moves.Growl){
        //starts here
        var newDefense =  moves.Growl - firstPokemon.defense;
        console.log(newDefense);
        //ends here
    }else{
    var newHealth = (firstPokemon.health + firstPokemon.defense) - (secondPokemon.attack + move);
    console.log(newHealth);
    }
}

edit: When I did moves.Growl - firstPokemon.defense || 0; it returned -4 instead of NaN which is what I wanted it to do, but the person that answered that removed the answer so this has been answered by whoever that guy was.

4
  • have you checked the values in those properties? Commented Mar 26, 2016 at 16:19
  • When getting NaN, you most often have an undefined variable somewhere in your code. Commented Mar 26, 2016 at 16:22
  • Can you tell what is the value of firstPokemon.defense when you get NaN Commented Mar 26, 2016 at 16:27
  • Your if and else statements have errors, You don't need to use parseInt or Number() Commented Mar 26, 2016 at 16:39

3 Answers 3

1

The problem is that you are adding the object in the second argument. Also your if statement will never execute, I have fixed both as following

        var myPokemon = {
    	health: 25,
    	defense: 5,
    	attack: 10,
    	speed: 5
    };
    
    var moves = {
    	Scratch: 5,
    	Bite: 5,
    	Slap: 5,
    	Growl: 1
    };
    
    
    var computerPokemon = {
    	health: 20,
    	defense: 5,
    	attack: 10, 
    	speed: 7
    };
    
    function calcDamage(firstPokemon, secondPokemon, move) {
    	if(moves.Growl!=undefined){
            //starts here
    		var newDefense =  moves.Growl - firstPokemon.defense;
    		alert(newDefense);
            //ends here
    	}else{
    	var newHealth = (Number(firstPokemon.health) + Number(firstPokemon.defense)) - (Number(secondPokemon.attack) + Number(move.Growl));
    	alert(newHealth);
        }
    }

calcDamage(myPokemon,computerPokemon,moves)

Sign up to request clarification or add additional context in comments.

6 Comments

also your if statement will not work (moves==moves.Growl) if you wanna check move.Growl exist then do it as moves.Growl!=undefined
I think he is checking the values of move and moves.Growl. And not trying to check if it exists are not
Okay but his if statement was not getting executed and in the else statement he was adding moves instead of a value from it
@SherKhan I think I found his issue, check my answer
@Reddy I already upvoted all the answers including yours, It's move because he is passing he named the third parameter as "move" not "moves", anyways I appreciated your effort, problem is that there are bugs in the code beyond just the NaN problem
|
1

Usually, if you are getting NaN, you are probably working with other elements but numbers. Are you sure they both are numbers?

Just an example:

var x = {}, y = {};
x.r = 10;
y.r = 5;
x.r - y.r; // yields 5

Comments

0

Use parseInt to convert the values into integer and then do the math.

 var value = parseInt(object1.objectProperty,10) - parseInt(object2.object2Property,10);

The Problem is here

var newHealth = (Number(firstPokemon.health) + Number(firstPokemon.defense)) - (Number(secondPokemon.attack) + Number(move.Growl));

The last one Number(move.Growl) it should be Number(moves.Growl) moves not move. In your case move is just a number and you are trying Number(move.Growl) which will be NaN and hence your getting NaN.

1 Comment

It's not parseInt problem, problem is that his code was not adding and running conditional statements properly but your answer is good for those who are trying to add strings

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.