Java Script Problem #3 Write a script that creates objects for people named Ani,
ID: 3809492 • Letter: J
Question
Java Script
Problem #3 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. Hint: http://www.w3schools.com/js/tryit.asp?filename=tryjs_create_object3 (Links to an external site.) console.log(sipho.mother); // tuulia ani.changeSpouse("mars"); console.log(ani.spouse); // mars
Explanation / Answer
function Person() {
this.name = "";
this.mother = "";
this.spouse = "";
this.children = [];
}
Person.prototype.changeSpouse = function(spouse)
{
this.spouse = spouse;
}
ani = new Person();
ani.name = "Ani";
sipho = new Person();
sipho.name = "Sipho";
tuulia = new Person();
tuulia.name = "Tuulia";
aolani = new Person();
aolani.name = "Aolani";
hiro = new Person();
hiro.name = "Hiro";
xue = new Person();
xue.anme = "Xue";
sipho.mother = tuulia.name
tuulia.children = [sipho.name]
ani.spouse = sipho.name;
sipho.spouse = ani.name;
aolani.mother=sipho.name
hiro.mother= sipho.name
xue.mother = sipho.name
aolani.father = ani.name
hiro.father=ani.name
xue.father=ani.name
sipho.children = [aolani.name, hiro.name, xue.name]
ani.children = [aolani.name, hiro.name, xue.name]
console.log(sipho.mother); // tuulia
ani.changeSpouse("mars");
console.log(ani.spouse); // mars
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.