2014-01-21 76 views
2

在開始Dev Bootcamp之前,我正在進行練習練習,儘管我的驅動程序代碼看起來應該是正確的,但它始終返回false而不是true。JS:驅動程序代碼返回false,我不知道爲什麼?

具體而言,編碼

greg.greet() 

返回

"Hello, Greg Varias! Glad to have another Island Fox! We'll see you in Summer 2014!" 

但代碼

greg.greet() === "Hello, Greg Varias! Glad to have another Island Fox! We'll see you in Summer 2014!"; 

返回

false 

我很抱歉,如果有一個明顯的答案,我只是沒有看到,但我是編程新手,我用盡了我的谷歌搜索能力和其他資源。很感謝任何形式的幫助!

這裏是我的代碼:行98仍返回false

function Cohort(cohortName, timeFrame){ 
    this.name = cohortName; 
    this.time = timeFrame; 
} 

function Student(studentName, cohortName){ 
    this.studentName = studentName; 
    this.cohortName = cohortName; 
    this.greet = greet 
} 

var greet = function(){ 
    console.log("Hello, " + this.studentName + "! Glad to have another " + this.cohortName.name + "! We'll see you in " + this.cohortName.time +"!"); 
} 

// driver code 

var islandFox = new Cohort("Island Fox", "Summer 2014"); 
islandFox.name === "Island Fox" //true 
islandFox.time === "Summer 2014" //true 

var greg = new Student("Greg Varias", islandFox); //undefined 
greg.cohortName.name === "Island Fox" //true 
greg.cohortName === islandFox // true 

console.log(greg.cohortName) //{ name: 'Island Fox', time: 'Summer 2014' } 

greg.greet() === "Hello, Greg Varias! Glad to have another Island Fox! We'll see you in Summer 2014!" //false 
greg.greet() //Hello, Greg Varias! Glad to have another Island Fox! We'll see you in Summer 2014! 

//驅動程序代碼,即使greg.greet()上線99似乎完美地打印出精確的短語。 ??

再次,任何幫助都非常讚賞和建設性的批評是值得歡迎的。我還在學習,如果我一直以錯誤的方式做事,我無法學習。謝謝!

回答

4

因爲問候不返回字符串!它沒有返回值回

console.log(greg.greet()) //undefined 

如果現在的功能均

var greet = function(){ 
    return ("Hello, " + this.studentName + "! Glad to have another " + this.cohortName.name + "! We'll see you in " + this.cohortName.time +"!"); 
} 

您的支票將是真實的。

+0

即,將某些內容打印到控制檯與返回它不同。 –

+0

完美!非常感謝!當我使用return和print/puts/console.log時,我仍然得到了一些時間,但我會做更多的學習。再次感謝! – ErinJoan

相關問題