As a starter of JS, I don't know what's wrong with my code. I guess the bug lies on notifyObserver() Because I use console.log at that point but it doesn't output anything. I don't know how to fix.
constructor(){
this.numberOfGuests=2;
this.subscribers=[];
}
setNumberOfGuests(x){
this.numberOfGuests = x;
this.notifyObservers({guests:x});
return x
// TODO TW1.1
}
getNumberOfGuests(){
return this.numberOfGuests // TODO TW1.1
}
addObserver(callback){
this.subscribers.push();
}
notifyObservers(whatHappened){
for(let i=0; i<this.subscribers.length; i++)
{
let callback= subscribers[i];
return callback
}
}
}
function onModelChange(payload){
document.body.textContent= payload.guests;
}
model.addObserver(onModelChange);
// initialize the view as before
document.body.textContent= model.getNumberOfGuests();