2017-03-09 1 views
1

Je devrais utiliser une seule fonction pour cela. Lorsque le bouton est cliqué, multipliez simplement chaque quantité par son prix respectif et ajoutez-les ensemble. Je ne suis pas sûr de savoir comment créer une seule fonction. J'ai ajouté des commentaires à chaque section pour aider à comprendre, je crée une page de commande de base. J'ai défini des valeurs de coût pour 5 articles, la quantité est un champ de texte numérique que l'utilisateur entrera dans un entier.JavaScript - Comment faire 6 fonctions de commande individuelles en une seule

Here's my code: 
    /*Array to hold QTY wanted from user*/ 
     var dvdQTY = [0, 0, 0, 0, 0]; 
     var movieName = new Array(5); 
     movieName[0] = "Star Wars"; 
     movieName[1] = "The Empire Strikes Back"; 
     movieName[2] = "Return of the Jedi"; 
     movieName[3] = "The Force Awakens"; 
     movieName[4] = "Rogue One"; 

     /*variables to store the values entered.*/ 
     var ep4Cost = 0; 
     var ep5Cost = 0; 
     var ep6Cost = 0; 
     var ep7Cost = 0; 
     var rogueCost = 0; 
     var totalEstimate = 0; 

     /* Array of the price variables*/ 
     var moviePrice = new Array(5); 
     moviePrice[0] = 65; //Price of EP4 
     moviePrice[1] = 55; //Price of EP5 
     moviePrice[2] = 45; //Price of EP6 
     moviePrice[3] = 35; //Price of EP7 
     moviePrice[4] = 25; //Price of Rouge One 

     /*Function to calculate the totalEstiamte of entered values by the user at a price of $65 dollars each.*/ 
     function calcEP4() { 
      totalEstimate -= ep4Cost; 
      dvdQTY[0] = document.movielist.dvdQTY[0].value; 
      ep4Cost = dvdQTY[0] * moviePrice[0]; 
      totalEstimate += ep4Cost; 
      +totalEstimate; 
      console.log(dvdQTY); 
     } 

     /*Function to calculate the totalEstiamte of entered values by the user at a price of $55 dollars each.*/ 
     function calcEP5() { 
      totalEstimate -= ep5Cost; 
      dvdQTY[1] = document.movielist.dvdQTY[1].value; 
      ep5Cost = dvdQTY[1] * moviePrice[1]; 
      totalEstimate += ep5Cost; 
      +totalEstimate; 
     } 

     /*Function to calculate the totalEstiamte of entered values by the user at a price of $45 dollars each.*/ 
     function calcEP6() { 
      totalEstimate -= ep6Cost; 
      dvdQTY[2] = document.movielist.dvdQTY[2].value; 
      ep6Cost = dvdQTY[2] * moviePrice[2]; 
      totalEstimate += ep6Cost; 
      +totalEstimate; 
     } 

     /*Function to calculate the totalEstiamte of entered values by the user at a price of $35 dollars each.*/ 
     function calcEP7() { 
      totalEstimate -= ep7Cost; 
      dvdQTY[3] = document.movielist.dvdQTY[3].value; 
      ep7Cost = dvdQTY[3] * moviePrice[3]; 
      totalEstimate += ep7Cost; 
      +totalEstimate; 
     } 

     /*Function to calculate the totalEstiamte of entered values by the user at a price of $25 dollars each.*/ 
     function calcRogue() { 
      totalEstimate -= rogueCost; 
      dvdQTY[4] = document.movielist.dvdQTY[4].value; 
      rogueCost = dvdQTY[4] * moviePrice[4]; 
      totalEstimate += rogueCost; 
      +totalEstimate; 
     }  

     /*Function to calculate the totalEstiamte and show the results to the button "total" on click.*/`enter code here` 
     function myTotal() 
     { 
      var newTotal = "$" + totalEstimate; 
      document.getElementById("total").innerHTML = "$" + totalEstimate; 
      console.log(newTotal) 

     } 

Répondre

0

Vous prenez ce qui varie d'une fonction à et transmettre ces valeurs en une seule fonction qui les utilise:

function calc(epCost, dvd, moviePrice) { 
     totalEstimate -= epCost; 
     dvd = document.movielist.dvd.value; 
     epCost = dvd * moviePrice; 
     totalEstimate += epCost; 
     +totalEstimate; 
    } 

Et vous passez ces valeurs lorsque vous devez appeler la fonction:

calc(ep6Cost, dvdQTY[2], moviePrice[2]);