-1

I am working on a simple dog object with a name, bark method, and loudBark method.

  • 'name' variable has to be global
  • 'dog' object should be in global scope
  • global variable 'loud' has to be used to create the 'loudBark' method
  • im trying to practice shorthand too

This is where I am currently at:

const name = 'Cody';
const loud = 'loud';

const dog = {
    bark() { return 'ruff ruff!' },
    name,
    ['${loud}Bark']: function () {
        return this.bark.toUpperCase();
    },
};

If I call 'dog.loudBark' it says that is not a function, and I'm really confused, especially since I'm just now learning this new syntax and am used to the older one; I forgot what version specifically.

Bergi
  • 630,263
  • 148
  • 957
  • 1,375
  • 1
    You're probably looking for `[loud + 'Bark']` or ``[`${loud}Bark`]`` - notice the backticks instead of apostrophes for the template string. – Bergi Aug 08 '20 at 22:46
  • Use backticks not single quotes around `${loud}Bark`. (Typically the character is in the top left of the keyboard, just left of the numbers.) – Robin Zigmond Aug 08 '20 at 22:46
  • 1
    [Duplicate](https://www.google.com/search?q=site%3Astackoverflow.com+js+template+literal+not+working) of [ES6 / ECMA6 template literals - not working](https://stackoverflow.com/questions/37245679/es6-ecma6-template-literals-not-working). – Sebastian Simon Aug 08 '20 at 22:59

1 Answers1

0

There's issue with your property assignment

To use string interpolation you need to place backticks(`), not singlequotes(')

so it will be

const dog = {
    bark() { return 'ruff ruff!' },
    name,
    [`${loud}Bark`]: function () {
        return this.bark.toUpperCase();
    },
};
Temoncher
  • 644
  • 5
  • 15