Why doesn't it work with string literal?

My code :

let dog = {
  name: "Spot",
  numLegs: 4,
  sayLegs: ()=>`This dog has+ ${numLegs} legs`
};
dog.sayLegs();
  **Your browser information:**

User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/90.0.4430.72 Safari/537.36.

Challenge: Create a Method on an Object

Link to the challenge:

this makes the string be “This dog has+ 4 legs”, isn’t there an extra character?


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 (’).

1 Like

Hi
you use the arrow function and it’s ok. But after that, you use Template literals instead of that, you can do this:

saylegs: ()=>{ return `This dog has `+ dog.numLegs+` legs.`}

The thing above is a concatenation.

Don’t forget some items:

  1. function needs open&close curly braces.
  2. use return keyword for output.
    3.in concatenation don’t forget the space before or after the strings.
    4.in objects try to use inside methods instead of template strings ${}

Good Luck. :four_leaf_clover:

this is not true, arrow functions allow for implicit return
this is a case of implicit return

Other things I missed

  • numLegs is an undeclared variable, notice how in the example it uses duck.name
  • You are asked to return This dog has 4 legs., you are also missing a character at the end of the string to be able to return the required output, other than having an extra character in the middle of it.

Changing those three things your code passes. You can solve this with arrow function implicit return and template literals.