jeudi 9 mars 2017

JavaScript - How to make 6 individual order total functions into one

I should be using a single function for this. When the button is clicked, simply multiply each quantity by its respective price and add them together. I'm not sure how to create a single function. I've added comments to each section to to help understand, I'm creating a basic ordering page. I have set values of cost for 5 items, the quantity is a number text field the user will enter in any whole interger.

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)

        }




Aucun commentaire:

Enregistrer un commentaire