I know this might be silly question and I might not asking this question the right way but I'm stuck on this part I keep trying to run this program and it says "Uncaught ReferenceError: useStudent is not defined" when I check the console on "Student" I check my script it's perfectly fine and my code
<script src="A2.js"></script>
<body>
<div class="column1">
<div class="input">
<button onclick="useStudent()"> Student</button>
<button onclick="useCar()"> Car</button>
</body>
</html>
here is my script added to this code
function useStudent(){
var stu = "Jon Lee";
var ye = 3;
console.log("year: " + ye);
var maj = "Math"
console.log("major: " + maj)
var message = stu.displayMe();
console.log(message);
console.log('-------------');
// set year to be 4
ye = 4;
// set major to be "test"
maj = "test";
// output year and major
console.log("year: " + ye);
console.log("major: " + maj);
message = stu.displayMe();
console.log(message);
}
here is displayme
displayMe() {
this.id + this.name + this.Year + this.Major
}
The "stu" type you defined is a string and it look like you need an object of student. therefore you need a conistractore function to create the student object and set the displayMe() method as its property.
function Stu (id,name, year,maj){
this.id = id;
this.name = name;
this.year = year;
this.major = maj;
this.displayMe = function(){ return this.id + this.name + this.year + this.major}
}
function useStudent(){
let stu = new Stu(1,"Jon Lee",3,'Math')
console.log("year: " + stu.year);
console.log("major: " + stu.maj);
console.log(stu.displayMe() );
console.log('-------------');
stu.maj = 'EECS';
stu.year = 4;
console.log("year: " + stu.year);
console.log("major: " + stu.maj);
console.log(stu.displayMe() );
console.log('-------------');
}