Answer the question
In order to leave comments, you need to log in
Why is the result from Observable not displayed?
I tried to implement Observable in native js, but I couldn't get the expected result. This code outputs undefined instead of the result. Please help me fix the code.
class Observable{
constructor(source) {
this.source = source.split('');
this.result = this.source;
}
subscribe(next) {
for(let item of this.result) {
next(item);
}
}
filter(predicate) {
this.result = this.result.filter(predicate);
return this;
}
map(callback) {
this.result = this.result.map(callback);
return this;
}
}
new Observable('qwerty')
.map((letter) => { letter.toUpperCase() })
.filter((letter) => { letter === 'W' })
.subscribe((letter) => { console.log(letter) });
Answer the question
In order to leave comments, you need to log in
This code outputs undefined instead of the result.
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question