我应该让你在队列中没有问题,因为最后三个问题我不知道为什么最后一个控制台应该返回你在队列中没有问题。它会返回当前问题还是我的代码有任何问题。
var student = {};
student.firstName = 'W';
student.lastName = 'H';
student.age = '21';
student.school = 'UofA';
student.questionQueue = [];
student.hasQuestion = false;
student.greeting = function(){return "Hello, my name is " + this.firstName;}
student.aboutMe = function(){return "My full name is " + this.firstName + " " + this.lastName + ", " + "I am " + this.age + " years old and I am a student at " + this.school;}
student.askQuestion = function(question){
this.questionQueue.push(question);
this.hasQuestion = !this.hasQuestion;
return "A question has been added to your queue";
}
student.answerQuestion = function(){
if(student.hasQuestion === true){
var currentQuestion = this.questionQueue.pop();
if (student.questionQueue.length === 0) {
student.hasQuestion === false;
}
return currentQuestion;
}
else if(student.hasQuestion === false){
return "You have no questions in your queue";
}
}
console.log('Greet Test:', student.greeting());
console.log('AboutMe Test:', student.aboutMe());
console.log('Answer Question Test:', student.answerQuestion());
console.log('Ask Question Test:', student.askQuestion('Why is the Earth flat?'));
console.log('Ask Question Test:', student.askQuestion('Why is the sky green?'));
console.log('Ask Question Test:', student.askQuestion('Why don\'t birds have wings'));
console.log('Answer Question Test:', student.answerQuestion());
console.log('Answer Question Test:', student.answerQuestion());
console.log('Answer Question Test:', student.answerQuestion());
console.log('Answer Question Test:', student.answerQuestion());
最佳答案
this.hasQuestion = !this.hasQuestion;
必须是
this.hasQuestion = true;
通过提问,您只是一次又一次地反转 bool 值。这没有任何意义。
和
if (student.questionQueue.length === 0)
student.hasQuestion === false;
必须是
if (student.questionQueue.length === 0)
student.hasQuestion = false;
您只是在测试 bool 值,同时您想要更改它。
关于javascript - 为什么最后一个函数没有返回你毫无疑问,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58595787/