Tell us what’s happening:
dog.sayLegs()
should return the given string - note that punctuation and spacing matter.
Your code so far
let dog = {
name: "Spot",
numLegs: 4,
sayLegs: function() {return "This dog has" + dog.numLegs + " legs.";}
};
dog.sayLegs();
Your browser information:
User Agent is: Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/87.0.4280.141 Safari/537.36.
Challenge: Create a Method on an Object
Link to the challenge:
I’ve edited your post for readability. When you enter a code block into a forum post, please precede it with a separate line of three backticks and follow it with a separate line of three backticks to make it easier to read.
You can also use the “preformatted text” tool in the editor (</>) to add backticks around text.
See this post to find the backtick on your keyboard.
Note: Backticks (`) are not single quotes (’).
sayLegs: function() {return "This dog has" + dog.numLegs + " legs.";}
The problems are here.
First, you are trying to access that number with dog.numLegs. But that doesn’t exist since it is being created. That’s OK - an object can refer to itself with the keyword this. So, inside the object, if I wanted to refer to the name property, it would be this.name.
The other problem is that with that fix, your string is still missing a space character (in the proper place).
When I make those two changes, your code passes for me.
I don’t think the curriculum covers this yes. The test passes if you simply copy/paste the sentence to return in the function.
Right, I stand corrected.
You do not need to use this at this point. The only problem was the space in the text string. I was getting ahead of myself.
As to just hardcoding the answer in, yes, that will work. That is a flaw in the test, but of course, if the goal is to learn, it would not be a good idea to exploit that flaw.
You are right as well @kevinSmith. The method should be used.