Intermediate Algorithm Scripting: Make a Person

if you use firstName as function parameter you can’t access the variable of same name define in the outer scope. How could the code know which of the two you are referring to?


this is just a suggestion to make your code avoud unnecessary variables, so it is not needed to make your code work but I think it is more elegant.

You have this:

 var a = firstAndLast.split(" ");
  var firstName = a[0];
  var lastName = a[1];

to avoid the use of the a variable you can do this:

var [firstName, lastName] = firstAndLast.split(" ");

this is called array destructing

other suggestion:
use let and const instead of var, as var is outdated for today standards.
so let [firstName, lastName] = firstAndLast.split(" ");

1 Like

Thanks again for your help and the useful suggestions on array destructuring and the use of let, which I have incorporated. I understand the point about not using the same name as the variable for the function parameter, but when I did this before, it couldn’t set the initial properties of the object to ‘Bob Ross’. I have changed it back as you can see below, but still can’t get the set functions to work.


what errors do you get?
your code seems fine

Yes, sorry my mistake ; I had copied the previous code block from my editor! I have removed the code, as you have previously asked others to do.

Thanks very much for your help.

Out of interest, why is this object constructed like this, as a function, rather than the way that I have read elsewhere about, i.e. like:

// Create an object:
var person = {
  firstName: "John",
  lastName : "Doe",
  language : "en",
  get lang() {
    return this.language;
  }
set lang(lang) {
    this.language = lang;
  }
};

What are the benefits of this?

if you have to create many objects with the same properties you have two ways:
you can create many objects, with typing
for example I am creatying the three objects bob, claire, teresa, following your structure: (I admit I have no experience with getters and setters so I am not including them just because I don’t know the syntax, but I can’t think of a reason for why it wouldn’t work for those too, knowing how to put them in the constructor)

var bob = {
  firstName: "Bob",
  lastName : "Morrigan",
  language : "en",
 
};

var claire = {
  firstName: "Claire",
  lastName : "Lacroix",
  language : "fr",
 
};

var teresa = {
  firstName: "Teresa",
  lastName : "Bianco",
  language : "it",
 
};

console.log(bob, claire, teresa);

See, we type a lot just to create three objects that have the same exact structure.
Or, we could instead use an object constructor:

var Person = function (firstName, lastName, lan) {
  this.firstName = firstName,
  this.lastName = lastName,
  this.language = lan,
  };

var bob = new Person("Bob", "Morrigan", "en");
var claire = new Person("Claire", "Lacroix", "fr");
var teresa = new Person("Teresa", "Bianco", "it");
console.log(bob, claire, teresa);

see, much less repetition, much less written code, much less danger of getting something wrong…

1 Like

Got it.
Thanks. That’s really great.
Sorry I was a bit slow. Just trying to learn something new during lockdown!

   <redacted solution>

It is great that you solved the challenge, but instead of posting your full working solution, it is best to stay focused on answering the original poster’s question(s) and help guide them with hints and suggestions to solve their own issues with the challenge.

We are trying to cut back on the number of spoiler solutions found on the forum and instead focus on helping other campers with their questions and definitely not posting full working solutions.

Thank you for understanding.