2017-03-09 55 views
1

私はこれに1つの機能を使用する必要があります。ボタンをクリックすると、各数量にそれぞれの価格を単純に乗算して加算します。 単一の関数を作成する方法がわかりません。私は理解するために各セクションにコメントを追加しました。私は基本的な注文ページを作成しています。私は5項目のコストの値を設定しました。量は、ユーザーがすべてのインターガーに入力する数値テキストフィールドです。JavaScript - 6個の個別注文合計機能を1つにする方法

あなたがそれらを使用する単一の関数にそれらの値を機能して渡すように機能ごとに異なるものを取る
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) 

     } 

答えて

0

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

そして、あなたは、関数を呼び出す必要があるときに、それらの値を渡します。

calc(ep6Cost, dvdQTY[2], moviePrice[2]); 
関連する問題