Solution 01
var addPreviousOwner = function(car,p_name,p_address) {
var o = { name : p_name, address : p_address }
if (car.previous_owners.length == 3) {
car.previous_owners.shift()
}
car.previous_owners.push(o)
}
addPreviousOwner(aCar,'Jim Nugent','3 Lower Road')
console.log(aCar.previous_owners[aCar.previous_owners.length - 1].name)
addPreviousOwner(aCar,'Rachel Fleming','4 Upper Road')
console.log(aCar.previous_owners[2].name)
console.log(aCar.previous_owners[0].name)
Solution 02
var aCar = {
owner : 'Joe Bloggs',
address : '3 Walkers Lane',
newOwner : function(p_name,p_address) {
this.addPreviousOwner(this.owner,this.address)
this.owner = p_name
this.address = p_address
},
. . . . . . . ..
}
Solution 03
var aCar = {
. . . . . .
wasOwnedBy : function(name_in) {
var result = false
this.previous_owners.forEach(function(owner) {
var name = owner.name.toUpperCase()
if (name_in.toUpperCase() == name) {
result = true
}
})
return result
},
. . . . . .
}
Solution 04
function Car(name_in,address_in,make_in,model_in,cc_in) {
. . . . . .
this.addPreviousOwner = function(p_name,p_address) { . . . . . . . }
this.newOwner = function(p_name,p_address) {
this.addPreviousOwner(this.owner,this.address)
this.owner = p_name
this.address = p_address
}
. . . . .
}