2015-05-08 175 views
0

我需要提示爲5個屬性,然後參加考試的分數,並得到試驗1,2和3的平均,然後顯示名稱和平均。我無法顯示或運行該功能。我的代碼有什麼問題?構造函數不工作

function Student(_firstName, _lastName, _t1, _t2, _t2){ 

    this.firstName = _firstName ; 
    this.lastName = _lastName ; 
    this.test1 = _t1 ; 
    this.test2 = _t2 ; 
    this.test3 = _t3 ; 

    this.fullName = function() { return this.firstName + " " + this.lastName } ; 

    this.calcAverage = function() { return (this.test1 + this.test2 + this.test3)/3 } ; 

} 

var name1 = prompt("Enter the first name:") ; 
var name2 = prompt("Enter the last name:") ; 
var te1 = parseInt(prompt("Enter the first test score:")) ; 
var te2 = parseInt(prompt("Enter the second test score:")) ; 
var te3 = parseInt(prompt("Enter the third test score:")) ; 

var person = new Student(name1, name2, te1, te2, te3) ; 

document.write(+name1+ " " +name2+ " " + person.calcAverage() +) ; 
+1

這是什麼呢?你在控制檯中有任何錯誤嗎? – krillgar

+1

你有很多錯誤。參數名稱是雙重的,document.write有不必要的'+'符號。 – com2ghz

回答

0

工作代碼...

function Student(_firstName, _lastName, _t1, _t2, _t3) { 

    this.firstName = _firstName; 
    this.lastName = _lastName; 
    this.test1 = _t1; 
    this.test2 = _t2; 
    this.test3 = _t3; 

    this.fullName = function() { 
     return this.firstName + " " + this.lastName 
    }; 

    this.calcAverage = function() { 
     return (this.test1 + this.test2 + this.test3)/3; 
    }; 

} 

var name1 = prompt("Enter the first name:"); 
var name2 = prompt("Enter the last name:"); 
var te1 = parseInt(prompt("Enter the first test score:")); 
var te2 = parseInt(prompt("Enter the second test score:")); 
var te3 = parseInt(prompt("Enter the third test score:")); 

var person = new Student(name1, name2, te1, te2, te3); 

document.write(+name1 + " " + name2 + " " + person.calcAverage()); 
0

解決您的錯別字:

function Student(_firstName, _lastName, _t1, _t2, _t3) { 
                ^^ _t3, not _t2 

document.write(+ name1 + " " + name2 + " " + person.calcAverage() +); 
       ^^ remove +          ^^ remove + 

DEMO