Reputation: 482
Hi I have a problem with comparing 2 variables:
console.log(simulation.population == 40000000); //true
console.log(simulation.initialInfectedNumber == 5); //true
console.log(simulation.population < simulation.initialInfectedNumber); //true
Both are numbers:
export class Seirds {
...
population: number;
initialInfectedNumber: number;
...
}
Can somebody explain how it works? (Because 40mln are not less than 5 :P), probably I have strings in this variables but how it is possible when this variables are numbers.
EDIT:
Get all simulation objects
service:
public getAllSimulations(): Observable<Seirds[]> {
return this.httpClient.get<Seirds[]>(this.url + '/all');
}
component method:
this.service.getAllSimulations().subscribe(
value => {
this.simulations = value;
}
API response:
[
{
"id": 1,
"name": "example name",
"population": 40000000,
"initialInfectedNumber": 5,
"daysOfSimulation": 2,
"reproductionRate": 3.0,
"immunityTime": 60.0,
"incubationTime": 4.0,
"naturalDeathRate": 0.0,
"quarantineRate": 0.5,
"birthRate": 0.0,
"diseaseDuration": 15.0,
"diseaseDeathRate": 0.1,
"reductionByRestrictions": 0.2,
"percentageOfPopulationWhenRestrictionsBegins": 1.0,
"daysOfRestrictions": 30.0,
"infectiousTime": 7.0,
"timeOfOnsetOfSymptoms": 5.0,
"timeOfDyingFromIncubation": 8.0,
"records": [
{
"susceptible": 39999995,
"exposed": 5,
"infected": 0,
"recovered": 0,
"deaths": 0,
"quarantined": 0
},
{
"susceptible": 39999993,
"exposed": 2,
"infected": 5,
"recovered": 0,
"deaths": 0,
"quarantined": 1
}
]
},
{
"id": 2,
"name": "example name",
"population": 40000000,
"initialInfectedNumber": 5,
"daysOfSimulation": 2,
"reproductionRate": 3.0,
"immunityTime": 60.0,
"incubationTime": 4.0,
"naturalDeathRate": 0.0,
"quarantineRate": 0.5,
"birthRate": 0.0,
"diseaseDuration": 15.0,
"diseaseDeathRate": 0.1,
"reductionByRestrictions": 0.2,
"percentageOfPopulationWhenRestrictionsBegins": 1.0,
"daysOfRestrictions": 30.0,
"infectiousTime": 7.0,
"timeOfOnsetOfSymptoms": 5.0,
"timeOfDyingFromIncubation": 8.0,
"records": [
{
"susceptible": 39999995,
"exposed": 5,
"infected": 0,
"recovered": 0,
"deaths": 0,
"quarantined": 0
},
{
"susceptible": 39999993,
"exposed": 2,
"infected": 5,
"recovered": 0,
"deaths": 0,
"quarantined": 1
}
]
}
]
service component:
import { HttpClient } from '@angular/common/http';
import { Injectable } from '@angular/core';
import { Observable } from 'rxjs';
@Injectable({
providedIn: 'root'
})
export class SeirdsService {
private url = 'http://localhost:8080/api/seirds';
constructor(private httpClient: HttpClient) { }
public getAllSimulations(): Observable<Seirds[]> {
return this.httpClient.get<Seirds[]>(this.url + '/all');
}
public addSimulation(simulation: Seirds): Observable<Seirds> {
return this.httpClient.post<Seirds>(this.url, simulation);
}
public updateSimulation(simulation: Seirds): Observable<Seirds> {
return this.httpClient.put<Seirds>(this.url, simulation);
}
public deleteSimulation(id: number): void {
let endpoint = "/" + id;
this.httpClient.delete(this.url + endpoint).subscribe();
}
}
export class SeirdsRecord {
susceptible: number;
exposed: number;
infected: number;
recovered: number;
deaths: number;
quarantined: number;
}
export class Seirds {
id: number;
name: string;
population: number;
initialInfectedNumber: number;
daysOfSimulation: number;
reproductionRate: number;
immunityTime: number;
incubationTime: number;
naturalDeathRate: number;
quarantineRate: number;
birthRate: number;
diseaseDuration: number;
diseaseDeathRate: number;
reductionByRestrictions: number;
percentageOfPopulationWhenRestrictionsBegins: number;
daysOfRestrictions: number;
infectiousTime: number;
timeOfOnsetOfSymptoms: number;
timeOfDyingFromIncubation: number;
records: SeirdsRecord[];
}
Upvotes: 0
Views: 3540
Reputation: 1309
You're having "problems" with simulation object and, first of all, you need to compare with ===
instead of ==
to compare values and types. Now, you have values on simulation object that represents a string and after that you're comparing string and numbers.
const simulation = { population: 40000000 , initialInfectedNumber: 5 };
console.log(simulation.population === 40000000); //true
console.log(simulation.initialInfectedNumber === 5); //true
console.log(simulation.population < simulation.initialInfectedNumber); // false
Edit:
Typescript will show a warning if you do not use types correctly and it won't allow you to compare strings and numbers. You need to "map" your payload received from your service HTTP.
In your code, you can do something like this:
this.service.getAllSimulations().subscribe(
values => { // <- I suppose this is an array with your payload and make sure pouplation and initialInfectedNumber are numbers
this.simulations = values.map((value) => new Seird(value.population, value. initialInfectedNumber));
}
)
And now you will have simulations as Seird[] but if my Seird constructor receives the correct types:
class Seird {
population: number;
initialInfectedNumber: number;
constructor(population: number, initialInfectedNumber: number) {
this.population = population;
this.initialInfectedNumber = initialInfectedNumber;
}
}
Thanks to @KennyXu for the comment that indicates typescript does not check at runtime. Make sure your API returns numbers.
Upvotes: 1