0

I am a beginner to Javascript and I am trying to make a text-based game for practice. I have made objects for enemies like so:

export const spider = {
strength: 1,
health: 4,
gold: 1
};

I am now trying to make a function which can search for their stats when their name is entered in the field. It looks like this:

export const findStats = (target) => {
return target.health;
}

However, when I do this and test it I get 'undefined.' However, if I replace 'target.health' with 'spider.health' it works properly.

The error occurs when I try doing findStats("spider") which gives me the 'undefined results.'

Any help would be appreciated. Note: I did look around this site but found the threads too complex or not quite what I was looking for. I am a beginner so simple terms would be greatly appreciated! Thanks for all your comments!

My issue is unlike to the one on the right as when I try it with my code is still returns 'undefined.' Here is the code that I have just tried using the other thread. Keep in mind I am a beginner so I am probably doing it very wrong but if you could help further I would be very happy.

export const findLocalStats = (target) => {
  var bar = 'strength';
  console.log(target[bar]);
}

Here are is my test command:

expect(findLocalStats("spider")).toEqual(1);

Furthermore, when I tried the code written in JSFiddle in my editor, it still gave the same error in the terminal which is 'undefined.'

export function findStats(target) {
return target.strength;
}

And the test command:

expect(findStats("spider")).toEqual(1);

And the exact error message:

findStats() gives the stats of the target wanted.

expect(received).toEqual(expected)

Expected value to equal:
  1
Received:
  undefined

Difference:

  Comparing two different types of values. Expected number but received undefined.

Sorry if this is very tedious. Note 2: I posted this question before but I have changed it to attempt to explain my issue better as I still do not understand the answers I was given and the links to duplicate questions - which I have tried to replicate. Furthermore, the duplicate that I was given was different. It used the object as the keyword, however I want to use it as a feed-in to a parameter so that it is versatile for more than one enemies if that makes sense. Thank you!

1
  • 2
    You're giving it a string, which doesn't have a health field. Just give spider,don't wrap it in quotes when you pass it. Commented Jul 4, 2017 at 15:22

1 Answer 1

1

When you are calling findStats like this : findStats("spider") you are sending a string to the function as an argument. That's why when it tries to get the property health it says undefined.

You need to give the function an instance of your spider object. To create an instance you can use the Object.create() method like var killMe = Object.create(spider). This will create a new object based on the one you declared before. Then you can call your function like this : findStats(killMe)

You can find an exemple here : https://jsfiddle.net/10ex1xqt/

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

Comments

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.