I have a random number between 1 and 20 at intervals. I need to add buttons to move up and down. (increment & decrement). I could not combine the intervals I have with my functions
The function that does a random number between 1 and 20:
floor!: Observable<Number>;
floorNumber = -1;
ngOnInit(): void {
this.floor = this.getNumbersInfinite();
}
getNumbersInfinite() {
return interval(1000)
.pipe(
map(() => Math.floor(Math.random() * 20) + 1),
tap(res =>this.floorNumber=res));
}
Unsuccessful implement of up and down:
up(){
if (Number(this.floorNumber) < 20 && Number(this.floorNumber) >= 1) {
return Number(this.floorNumber++);
}
return false;
}
down(){
if (Number(this.floorNumber) < 20 && Number(this.floorNumber) >= 1) {
return Number(this.floorNumber--);
}
return false;
}
html:
<p>
{{ floor | async }}
</p>
<input type="button" value="Up" (click)="up()" />
<input type="button" value="down" (click)="down()" />