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
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
};
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.
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.
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. Nice day. Sorry.