Skip to content

Instantly share code, notes, and snippets.

@irwansyahwii
Created October 7, 2017 14:43
Show Gist options
  • Save irwansyahwii/91f2f279235c6481f4590ca54444702c to your computer and use it in GitHub Desktop.
Save irwansyahwii/91f2f279235c6481f4590ca54444702c to your computer and use it in GitHub Desktop.
Traveloka Bus Riddle
import * as Rx from 'rxjs/Rx';
let currentBus:string = "";
let Bus106Interval = Rx.Observable.interval(3000).map(() => currentBus = ('106'));
let Bus77Interval = Rx.Observable.interval(3000).map(() => currentBus = ('77'));
Bus77Interval.toPromise();
setTimeout(() => {
Bus106Interval.toPromise();
}, 1000);
function randomDelay(bottom:number, top:number) {
return Math.floor( Math.random() * ( 1 + top - bottom ) ) + bottom;
}
Rx.Observable.interval(500)
.delay(randomDelay(1000, 3000))
.map(() => {
if(currentBus.length > 0){
console.log(`Taking bus #${currentBus}`);
}
}).toPromise();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment