Im trying this further exercise and have finished my code to change the bio alert to account for gender and more than two hobbies. I have opted to use functions that are called within the bio but this means I cant use ‘this’ within the functions (I assume this is because the scope of ‘this’ does not extend beyond the function within constructor?). I therefore use the direct parameter fed into the constructor in my if statements rather than this. Is this a good way to approach the problem or would be be better to use the method they suggest in the competed example?
function Person(first, last, age, gender, interests) {
this.name = {
first : first,
last : last
};
this.age = age;
this.gender = gender;
this.interests = interests;
function pronoun(){
if (gender === 'm'){
return 'He';
} else {
return 'She';
}
}
function listInterest(){
if(interests.length === 1) {
return interests[0] + '.';
} else if(interests.length === 2) {
return interests[0] + ' and ' + interests[1] + '.';
} else {
let sentence = ''
for(var i = 0; i < interests.length; i++) {
if(i === interests.length - 1) {
sentence += 'and ' + interests[i] + '.';
} else {
sentence += interests[i] + ', ';
}
}
return sentence
}
}
this.bio = function() {
alert(this.name.first + ' ' + this.name.last + ' is ' + this.age + 'years old. ' + pronoun() + ' likes ' + listInterest());
};
this.greeting = function() {
alert('Hi! I\'m ' + this.name.first + '.');
};
}
let person1 = new Person('Bob', 'Smith', 32, 'm', ['music' , 'skiing' , 'dancing']);