5/7. Total is exact in the console but the Error message is false


#1


https://www.codecademy.com/fr/courses/javascript-beginner-fr-FR-4ttc1/0/5?curriculum_id=5355c2b7516bcbd912000008#


var caisse = {
    total: 0,
    prixDerniereTransaction: 0,
    //N'oubliez pas d'ajouter votre propriété 
    
    ajouter: function(prixElement) {
        this.total += prixElement;
        this.prixDerniereTransaction += prixElement;
    },
    scan: function(element, quantite) {
        switch (element) {
        case "oeufs": this.ajouter(0.98 * quantite); break;
        case "lait": this.ajouter(1.23 * quantite); break;
        case "magazine": this.ajouter(4.99 * quantite); break;
        case "chocolat": this.ajouter(0.45 * quantite); break;
        }
        return true;
    },
    //Ajoutez la méthode annuleDerniereTransaction ici 
      annuleDerniereTransaction: function(){
       this.total -= this.prixDerniereTransaction;
    }
};


caisse.scan('oeufs',1);
caisse.scan('lait',1);
caisse.scan('magazine',1);
caisse.scan('chocolat',4);

//Annulez la dernière transaction et ajoutez 3 à la place
caisse.annuleDerniereTransaction();
caisse.scan("chocolat", 3);


// Affichez le total de la note
console.log('Votre total est de '+ caisse.total);

Oups, merci de réessayer. false 
Votre total est de 1.35

Is it a BUG?

## Titre


#2

With your code total has with every transaction the same value like prixDerniereTransaction

this.prixDerniereTransaction += prixElement;

Here you set the value from total to 0

annuleDerniereTransaction: function(){
       this.total -= this.prixDerniereTransaction;
    }

And with

caisse.scan("chocolat", 3);

you set total to a value of 3 times 0.45 = 1.35

But this is not correct you only wanna delete the last Transaction

this.prixDerniereTransaction = prixElement;

gives you what you want


#3

Thanks a lot.
I have a problem with 3/7.
I don't see my mistake.


#4

@snakefinger33gmail.c

have a look at the following line

this.prixDerniereTransaction += prixElement;

You have

  +=

here instead of just an '=' which will in turn increase the discount amount and you'll get the wrong output

i.e 1.35

instead of


8.55


#5

I show you my code for the 3/7.

https://www.codecademy.com/fr/courses/javascript-beginner-fr-FR-4ttc1/0/3?curriculum_id=5355c2b7516bcbd912000008#

This is my code :

var caisse = {
total:0,
// Ecrivez la méthode ajouter ci-dessous :
ajouter: function(coutElement){
this.total += coutElement;
}
};

scan: function(element) {
    switch(element) {
    case "oeufs": 
        this.ajouter(0.98); 
        break;

    case "lait": 
        this.ajouter(1.23); 
        break;

    // Ajoutez deux autres éléments ci-dessous
    case "magazine":
        this.ajouter(4.99);
        break;

    case "chocolat":
        this.ajouter(0.45);
        break;

    }
    return true;
}

};

// Scannez 2 oeufs et 3 magazines
caisse.scan("oeufs");
caisse.scan("oeufs");
caisse.scan("magazine");
caisse.scan("magazine");
caisse.scan("magazine");
// Affichez le total
console.log('Votre total est de '+ caisse.total);

Oups, merci de réessayer. There was a problem with your syntax.

SyntaxError: function statement requires a name


#6

Thank you very much.
It's OK.


#7

Everything is good now?


#8

Hi codmakr.
Everything is allright now.
I am learning PHP now.
Thank you for your help.


#9

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.