Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

JavaScript

Henrik Christensen
seal-mask
.a{fill-rule:evenodd;}techdegree
Henrik Christensen
Python Web Development Techdegree Student 38,322 Points

valid :id

Hi,

I'm working on an Angular project, and it works for now, but I'm having this feeling that there has to be a better / more elegant way to check if the :id param is within the desired range?

    constructor(private http: Http, private activatedRoute: ActivatedRoute, private router: 
    Router) {}

    ngOnInit() {
        // getting current params
        this.activatedRoute.params.subscribe((params: Params) => {
            const id: number = params['id']; // storing the id-param
            if (id > 0 && id <= 4) {
                // getting data from API if the id is ]0;4]
                this.http.get('http://localhost:5000/api/alkanes')
                .map((res: Response) => res.json())
                .subscribe(data => {
                    data.map(alkane => {
                        this.alkanes.push(alkane);
                        this.names.push(alkane.name);
                        this.choices.push(this.turnToFormula(alkane.formula));
                    });
                    this.shuffle(this.names);
                    this.shuffle(this.choices);
                });
            } else {
                // redirects back to /alkan-spil if id is not ]0;4]
                this.router.navigate(['alkan-spil']);
            }
        });
    }