Reputation: 43
I'm working on a practice exercise as a review before I start Dev Bootcamp, and even though my driver code looks like it should be correct, it keeps returning false instead of true.
Specifically, the code
greg.greet()
returns
"Hello, Greg Varias! Glad to have another Island Fox! We'll see you in Summer 2014!"
but the code
greg.greet() === "Hello, Greg Varias! Glad to have another Island Fox! We'll see you in Summer 2014!";
returns
false
I apologize if there is an obvious answer that I'm just not seeing, but I'm new to programming and I've exhausted my google searching ability and other resources. Any help is very much appreciated!
Here's my code:
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!
//driver code on line 98 still returning false, even though greg.greet() on line 99 seems to print the exact phrase perfectly. ??
Again, any help at all is very much appreciated and constructive criticism is welcome. I'm still learning, and I can't learn if I keep doing things the wrong way. Thank you!
Upvotes: 2
Views: 97
Reputation: 207501
because greet does not return a string! It returns nothing back
console.log(greg.greet()) //undefined
Now if the function were
var greet = function(){
return ("Hello, " + this.studentName + "! Glad to have another " + this.cohortName.name + "! We'll see you in " + this.cohortName.time +"!");
}
your check would be true.
Upvotes: 4