I've been trying to figure out this error in JS and cant understand why its happening. The program is supposed to inherit properties from the person object and add two additional properties, sID and courses. The for loop is then supposed to push values that the user inputs(a minimum of 5) into the properties. The problem I have though is that once it reaches the courses property, which is an array, I keep getting an error saying that courses is undefined. Any help is greatly appreciated.
// person object
var person = { firstname: "", lastname: "", email: "" };
var student = {};
student.prototype = person;
student.prototype.sid = "";
student.prototype.courses = {};
/******************************************************************************
* your code starts here
*****************************************************************************/
var answer = null;
function getanswer(string) {
answer = string;
return answer;
}
do {
getanswer(prompt("Please enter your first name, last name, student ID, email and courses (seperated by ',')"));
var array = answer.split(',');
answer = array;
for(var i = 0; i <= answer.length; i++) {
if (i < 4) {
student[i] = answer[i];
alert(student[i]);
}
if(i > 3) {
student.courses[i] = answer[i];
alert(student.courses[i]);
}
}
} while(answer !== null || answer !== "" );