Problem #3 Write a script that creates objects for people named Ani, Sipho, Tuul
ID: 3738112 • Letter: P
Question
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
<!DOCTYPE html>
<html>
<head>
<title>Objects</title>
<meta charset="UTF-8" />
<script type="text/javascript">
var peopleObject = new Object();
peopleObject['xue'] = {"name":"Xue"};
peopleObject['hiro'] = {"name":"Hiro"};
peopleObject['aolani'] = {"name":"Aolani"};
peopleObject['sipho'] = {"name":"Sipho"};
peopleObject['ani'] = {"name":"Ani"};
peopleObject['tuulia'] = {"name":"Tuulia"};
peopleObject['tuulia']['children'] = [peopleObject['sipho']];
peopleObject['sipho']['mother'] = [peopleObject['tuulia']];
peopleObject['sipho']['spouse'] = [peopleObject['ani']];
peopleObject['ani']['spouse'] = [peopleObject['sipho']];
peopleObject['sipho']['children'] = peopleObject['ani']['children'] =
[peopleObject['xue'], peopleObject['hiro'], peopleObject['aolani']];
peopleObject['xue']['father'] = peopleObject['hiro']['father'] = peopleObject['aolani']['father'] = [peopleObject['sipho']];
peopleObject['xue']['mother'] = peopleObject['hiro']['mother'] = peopleObject['aolani']['mother'] = [peopleObject['ani']];
function doShow() {
var peopleString = "";
for(var key in peopleObject) {
peopleString += "<fieldset><legend>" + peopleObject[key]['name'] + "</legend>";
if(peopleObject[key]['mother'] != undefined) {
peopleString += "<label>Mother:</label>";
peopleString += peopleObject[key]['mother'][0]['name'] + "<br />";
}
if(peopleObject[key]['father'] != undefined) {
peopleString += "<label>Father:</label>";
peopleString += peopleObject[key]['father'][0]['name'] + "<br />";
}
if(peopleObject[key]['spouse'] != undefined) {
peopleString += "<label>Spouse:</label>";
peopleString += peopleObject[key]['spouse'][0]['name'] + "<br />";
}
if(peopleObject[key]['children'] != undefined) {
peopleString += "<label>Children:</label>";
for(var cKey in peopleObject[key]['children']) {
if(cKey > 0) {
peopleString += ", ";
}
peopleString += peopleObject[key]['children'][cKey]['name'];
}
}
peopleString += "</fieldset>";
}
document.getElementById('people').innerHTML += peopleString;
}
</script>
<style type="text/css">
body {
background-color: #999999;
}
label {
width: 6em;
float: left;
text-align: right;
margin-right: 0.5em;
font-weight: bold;
}
</style>
</head>
<body>
<fieldset id="people">
<legend>People</legend>
</fieldset>
</body>
</html>
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.