Why SyntaxError: Invalid shorthand property initializer

Hi, I follow along a coding adventure game. And what works fine on my local machine is not accepted as solution. I got this error message “SyntaxError: Invalid shorthand property initializer” and I don’t see invalid notation but just a working code (locally) What do I miss? What do I have to look for?

Cheers and thanks!

function construct(name, material, assemble, duration) {
  let person = {
    name: name,
    material: material,
    assemble: assemble,
    duration: duration
  };
  return person;
}

const somePerson = construct("Kevin", "Human", true, 1000);
console.log(somePerson.name); // should be "Kevin"
console.log("duration is: " + somePerson.duration); // should be 1000

Which browser are you seeing this error in?
The code you have posted is error free and should run as expected.

Also, FWIW, you can tidy that up a little using modern syntax (ES6):

function construct(name, material, assemble, duration) {
  return {
    name,
    material,
    assemble,
    duration,
  };
}

const somePerson = construct('Kevin', 'Human', true, 1000);

console.log(somePerson.name);
console.log(`duration is: ${somePerson.duration}`);

It’s that modern ES6 syntax that I think is the shorthand that is being complained about by the website’s interpreter.

I recommend trying to resolve matters by putting the property keys in quotes:

  let person = {
    "name": name,
    "material": material,
    "assemble": assemble,
    "duration": duration
  };

And if issues continue, please let us know of the page where this is happening so that a deeper investigation can take place.

1 Like

But as long as they are valid identifiers (i.e. they don’t contain spaces or they aren’t reserved words) quotes around property names are optional, right?

The OP had:

let person = {
  name: name,
  material: material,
  assemble: assemble,
  duration: duration
};

Which IMO, should be fine.

Or am I missing something?

Yes normally that’s fine, but in this case he’s following along with a coding adventure game. The only reasonable reason why I think that he would be getting his syntax error is if the website does separating parsing of your code before it is submitted for verification. Given that situation you need to play along with the site’s expectations instead.

1 Like

Yup, fair point.

Another reason that might explain things is if you accidentally replace a colon with an equals sign instead.

It’s a desktop app not browser based. I pasted the task under the bold text. Maybe I completely misunderstood the question to to lack of English or brain.
Puttinq “keys” into quotes did not work.
The task
:

Create a file called construction.js in your code folder. Inside that file, create a function called construct that returns an object literal with the following properties:

Property Type Value Notes
name string See Notes This will be the first argument passed to your construct function
material string ‘human’
assemble boolean true
duration number 1000

Here is some code you can use as a starting point - it already has the function defined, but doesn’t yet work as described in the objective:

function construct(name) {
  let person = {};

  return person;
}

// The following lines of code are not required for the solution, but can be
// used by you to test your solution.
const somePerson = construct('Kevin');
console.log('name is: ' + somePerson.name); // should be "Kevin"
console.log('duration is: ' + somePerson.duration); // should be 1000

Thanks for your patience. I think I got sth. terrible wrong. Looking forward for your suggestions. :slight_smile:

Can you please link us to the site that gives you the “Create a file called construction.js” instructions so that we can try out a few ideas ourself?

This is really embarrassing. I am totally sorry for wasting your time. I just wanted to upload the file, when I recognized, I had an old and wrong code – with by all means wrong es6 shorthand code - in the correct folder. And the correct code – the one I posted here and you both took a look on it – in the wrong folder. So it could not be evaluated. Could we please simply forget this? Sorry. :weary: Nice day. Sorry.

3 Likes

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.