Struggling with a calculation formula to find the percentage using Angular and Typescript with Angularfire for database storage. Encountered an error stating
First argument contains NaN in property 'percent.percentKey.percentMale
. The properties are defined in a class as shown below.
//app.ts
export class App {
$key: string;
gender: string;
male: number = 0;
female: number = 0;
percentMale: number = 0;
percentFemale: number = 0;
}
//app.service.ts
insertdata(data: App) {
this.dataList.push({
gender: data.gender,
});
if (data.gender == 'Male') {
//this.male ++;
data.male = data.male++
} else if (data.gender == 'Female') {
//this.female ++;
data.female = data.female++
}
data.percentMale = data.male / (data.male + data.female) * 100
console.log('male', data.male + '%');
data.percentFemale = data.female / (data.male + data.female) * 100
console.log('female', data.female + '%');
console.log('1', data.percentMale); //it returns NaN
console.log('2', data.percentFemale); //it returns NaN
this.percentList.update("percentKey", {
percentMale: data.percentMale,
percentFemale: data.percentFemale
})
}
The class is successfully imported into the service, and the insertdata
function is being called from component.ts like
this.AppService.insertpertanyaan(this.AppService.selectedpertanyaan);
.
Seeking assistance to resolve this issue. Please let me know if additional code snippets are required. Thank you.