Reputation:
I'm learning Javascript and trying to complete the following exercise:
"Write a script that creates objects for people named Ani, Sipho, Tuulia, Aolani, Hiro, and Xue, such that:
Tuulia is the mother of Sipho. Ani and Sipho are married. The children of Ani and Sipho are, in order, Aolani, Hiro, and Xue. Define each of the person objects with as many of the following properties as you can fill in: name, mother, father, spouse, and children. The childrenproperty should have an array value. Also create a method for the person object that allows the spouse property to be changed.
console.log(sipho.mother);
// tuulia
ani.changeSpouse("mars");
console.log(ani.spouse);
// mars"
The code I have so far is:
var ani = {name: 'Ani', children:[]};
var tuulia = {name: 'Tuulia'};
var sipho = {name: 'Sipho', mother: 'Tuulia', spouse: 'Ani', children:[]};
var aolani = {name: 'Aolani', mother: 'Ani', father: 'Sipho'};
var hiro = {name: 'Hiro', mother: 'Ani', father: 'Sipho'};
var xue = {name: 'Xue', mother: 'Ani', father: 'Sipho'};
this.changeSpouse = function (spouse) {
this.spouse = name;
}
console.log(sipho.mother);
ani.changeSpouse("mars");
console.log(ani.spouse);
so, right now I'm able to get the mother name for console.log(sipho.mother).
I'm having trouble with creating a function to change the spouse name. Could anyone please point where I'm going wrong with this?
Also, I'm not entirely sure if I'm doing "the childrenproperty should have an array value." correctly.
Upvotes: 0
Views: 140
Reputation: 1
function FamilyTree(name,mother,father,spouse,children) {
this.name = name;
this.mother = mother;
this.father = father;
this.spouse = spouse;
this.children = children;
this.changeSpouse = (newSpouse) => this.spouse = newSpouse; /* You can add
that function here but its not necessery see below*/
}
let ani = new FamilyTree('ani');
let tuulia = new FamilyTree('Tuulia');
let sipho = new FamilyTree('Sipho','Tuulia',null,'Ani');
let aolani = new FamilyTree('Aolani','ani','Sipho');
let hiro = new FamilyTree('Hiro','Ani','Sipho');
let xue = new FamilyTree('Xue','Ani','Sipho');
// ani.spouse = 'Mars';
// console.log(ani);
/* you can achieve it this way ^^^ which is much simpler :)
but below is how the method would work */
ani.changeSpouse('mars');
console.log(ani)
Upvotes: 0
Reputation: 366
A couple comments:
#1 - You are implementing the children property correctly. It is an array which stores the person's children inside it.
#2 - I'm not sure if your exercise requires you to store a string of the name of the persons mother father etc., or the actual mother/father object itself (more specifically a pointer to that object - since an object is a reference type) i.e.
var xue = {name:'Xue', mother:ani /*pointer to ani object*/, father: etc.
#3 - you are having trouble with the changeSpouse function because you are declaring it within the global scope. If you want ani to have a method changeSpouse, you need to move it into the ani object like so
var ani = {
name:'Ani',
children:[],
changeSpouse: function (spouse) {
this.spouse = spouse;
}
#4 - if you are repeatedly making objects of the same structure, consider creating a class. You may have not learnt about them yet, and if so, don't worry about it. Here is an example implementation
class Person {
constructor(name, mother, father, children=[], spouse=null) {
this.name = name;
this.mother = mother;
this.father = father;
this.children = children;
this.spouse = spouse;
}
changeSpouse(spouse) {
this.spouse = spouse;
}
}
and to declare a new instance
let ani = new Person('Ani', /*other parameters go here*/);
Upvotes: 2