0

I have this code right here that returns NaN for some reason, but when I console.log parts of the return it returns an actual number. I tried Math.floor,parseInt`, I don't know what to do...

nouveauVoyage.prototype.prixGlobalParPersonne = function(){
    var frais=0;
    if (this.voiturePlaces == 0 && this.hotelPlaces == 0){
        frais=0;
    }else if(this.voiturePlaces != 0 && this.hotelPlaces != 0){
        frais=40;
    }else if((this.voiturePlaces != 0 && this.hotelPlaces == 0) || (this.voiturePlaces == 0 && this.hotelPlaces != 0)){
        frais=20;
    }
    return ((this.nbrAdultes * this.prixParPersonne) + ((this.prixParPersonne * this.nbEnfants) * 0,8) + frais) / (this.nbAdultes + this.nbEnfants);
};

for more details here's my constructor

function nouveauVoyage(type,destination,nbrAdultes,nbrEnfants,prixParPersonne,prixParGroupe,placesVoitures,placesHotel){
        this.id = (new Date().valueOf() + " ").substring(6, 13);
        this.type = type;
        this.destination = destination;
        this.nbrAdultes = parseInt(nbrAdultes);
        this.nbrEnfants = parseInt(nbrEnfants);
        this.prixParPersonne = parseInt(prixParPersonne);
        this.prixParGroupe = parseInt(prixParGroupe);
        this.voiturePlaces = parseInt(placesVoitures);
        this.hotelPlaces = parseInt(placesHotel);
    }

and what I pass to it

var type = $("select").first().val();
var destination = $("input:radio:checked").val();
var nbAdultes = $("input[type=number]").eq(1).val();
var nbEnfants = $("input[type=number]").eq(2).val();
var prixParPersonne = $("input[type=text]").first().val();
var prixParGroupe = $("input[type=text]").last().val()
prixParPersonne = prixParPersonne.substring(0, prixParPersonne.length-1);
prixParGroupe = prixParGroupe.substring(0, prixParGroupe.length-1);
var placesVoiture = $("input[type=number").eq(-2).val();
var placesHotel = $("input[type=number]").last().val();
var voyage = new nouveauVoyage(type,destination,nbAdultes,nbEnfants,prixParPersonne,prixParGroupe,placesVoiture,placesHotel);
Chase Roberts
  • 8,262
  • 10
  • 64
  • 118
  • 2
    Please post a full example we can debug. There are too many variables there whose value we don't know. – deceze May 23 '18 at 12:39
  • I think that maybe you are dividing by 0 (so this.nbAdultes + this.nbEnfants == 0 ?) – Ludovic Feltz May 23 '18 at 12:41
  • 1
    For one though, you probably mean `0.8`, not [`0,8`](https://stackoverflow.com/q/3561043/476). – deceze May 23 '18 at 12:41
  • I changed it but it didn't remove the probblem :/, maybe it's the division I honestly don't know – PostMaloneM3m3r May 23 '18 at 12:45
  • 1
    Might be because you are dividing it by the sum of (nbEnfants and nbAdultes) which is not available for nouveauVoyage. Please change all nbEnfants to nbrEnfants and all nbAdultes to nbrAdultes inside the function prixGlobalParPersonne – Kumar Aman May 23 '18 at 12:56
  • oops I haven't seen this, thanks – PostMaloneM3m3r May 23 '18 at 12:58
  • `NaN` means that you don't have a number. Most likely your `parseInt` call is failing somewhere. Maybe you are passing in a `"0,8"` instead of a `"0.8"` somewhere... – Chase Roberts May 23 '18 at 21:49
  • I've found the problem it was mispelling of property, I don't know how to mark @KumarAman answer as correct – PostMaloneM3m3r May 23 '18 at 22:40

0 Answers0