2016-07-20 13 views
0

Mein Ziel mit diesem Code ist ein Notbehelf in den Warenkorb zu erstellen, die die Möglichkeit gibt, aus einer kleinen Auswahl von Artikeln zu wählen, Ihre Quantität wählen, Ihre Gesamt, entfernen Artikel wenn Sie wollen und gehen Sie zur Kasse. Mein Problem ist, sobald ich die Ergebnisse des Preises durch den Typ des Artikels (per Alarm) erhalten habe, weiß ich nicht, wie man alle möglichen Warnungen zusammenfügt, da es in einer for-Schleife läuft. Hat jemand eine Idee, wie man das macht? Ich bin ziemlich neu in JS und weiß nicht weiter. Dies ist auch meine erste Frage hier auf Stack, also vergib mir, wenn es eine komische Frage ist.die Ergebnisse mehrerer Warnungen hinzufügen, die in einer for-Schleife ist

Hier ist mein Code:

var item = function(itemName, itemPrice, itemTax) { 
    this.products = itemName; 
    this.cost = itemPrice; 
    this.taxes = itemTax; 
    this.total = itemPrice + (itemPrice * itemTax) 
} 

var list = []; 

list[0] = new item("Shoes", 67.99, .075); 
list[1] = new item("Coat", 78.99, .075); 
list[2] = new item("Book", 9.99, .075); 
list[3] = new item("Suitcase", 56.99, .075); 

function theStore() { 
    var greeting = prompt("Welcome to xxxxxxxx! Do you want to begin shopping?"); 



    while (greeting === "Yes") 
    { 

     for (var i = 0; i < list.length; i++) 
     { 
     var j = list[i]; 
     var adding = prompt("The " + j.products + " cost " + j.cost + ". Would you like to add it to your cart?") 
     if (adding === "Yes") 
     { 
      var addingMore = prompt("How much do you want?") 
      if (addingMore < 1) 
      { 
      alert("This item was not added to your cart.") 
      } 

      else 
      { 
      alert(addingMore + " of " + j.products +" has been added to your cart." + " Your total for this kind of item is " + addingMore * (j.cost) + ".") 
      } 

     } 
     else 
     { 
     alert(j.products + " was not added to your cart.") 
     } 
    } 

    greeting = prompt("Do you want to continue shopping? Yes or No"); 


    } 
} 

theStore() 

Idealfall, wenn der Benutzer aufgefordert, „Nein“ (Linie 47) Ich möchte, dass sie die Summe aller Einzelteile zeigen.

Jede Hilfe/Tipps/Beratung ist willkommen.

Antwort

0

Deklarieren Sie eine Variable TotalAmount außerhalb der Schleife, und um es hinzuzufügen, wenn der Benutzer die Dinge in den Warenkorb hinzufügt. Wenn der Benutzer dann keine Warnung erhält, wird der Gesamtbetrag angezeigt.

Ich habe eine Zwischensummenvariable hinzugefügt, die berechnet wird, wenn der Benutzer die Anzahl der Elemente auswählt. Die Zwischensumme wird der Variable totalAmount hinzugefügt, die am Ende alarmiert wird.

var item = function(itemName, itemPrice, itemTax) { 
    this.products = itemName; 
    this.cost = itemPrice; 
    this.taxes = itemTax; 
    this.total = itemPrice + (itemPrice * itemTax) 
} 

var list = []; 
var totalAmount = 0; 

list[0] = new item("Shoes", 67.99, .075); 
list[1] = new item("Coat", 78.99, .075); 
list[2] = new item("Book", 9.99, .075); 
list[3] = new item("Suitcase", 56.99, .075); 

function theStore() { 
    var greeting = prompt("Welcome to xxxxxxxx! Do you want to begin shopping?"); 



    while (greeting === "Yes") 
    { 

     for (var i = 0; i < list.length; i++) 
     { 
     var j = list[i]; 
     var adding = prompt("The " + j.products + " cost " + j.cost + ". Would you like to add it to your cart?") 
     if (adding === "Yes") 
     { 
      var addingMore = prompt("How much do you want?") 
      if (addingMore < 1) 
      { 
      alert("This item was not added to your cart.") 
      } 

      else 
      { 
      var subTotal = addingMore * (j.cost); 
      totalAmount += subTotal; 
      alert(addingMore + " of " + j.products +" has been added to your cart." + " Your total for this kind of item is " + subTotal.toString() + ".") 
      } 

     } 
     else 
     { 
     alert(j.products + " was not added to your cart.") 
     } 
    } 

    greeting = prompt("Do you want to continue shopping? Yes or No"); 

    if(greeting === "No"){ 
     alert("Your Total Amount is: " + totalAmount); 
    } 

    } 
} 

theStore() 
0
var item = function(itemName, itemPrice, itemTax) { 
    this.products = itemName; 
    this.cost = itemPrice; 
    this.taxes = itemTax; 
    this.total = itemPrice + (itemPrice * itemTax) 
} 

var list = []; 

list[0] = new item("Shoes", 67.99, .075); 
list[1] = new item("Coat", 78.99, .075); 
list[2] = new item("Book", 9.99, .075); 
list[3] = new item("Suitcase", 56.99, .075); 

    var selected_items = []; 

    function theStore() { 
     var greeting = confirm("Welcome to xxxxxxxx! Do you want to begin shopping?"); 



     while (greeting) 
     { 

      for (var i = 0; i < list.length; i++) 
      { 
      var j = list[i]; 
      var adding = confirm("The " + j.products + " cost " + j.cost + ". Would you like to add it to your cart?") 
      if (adding) 
      { 
       var addingMore = prompt("How much do you want?") 
       if (addingMore < 1) 
       { 
       alert("This item was not added to your cart.") 
       } 

       else 
       { 
       alert(addingMore + " of " + j.products +" has been added to your cart." + " Your total for this kind of item is " + addingMore * (j.cost) + "."); 
    j.quantity = addingMore;    // Add user selected quantity to object 

    j.total_cost = addingMore * (j.cost); // Add cost if user selected quantity to object 

    selected_items.push(j);    // Let's store it in array. 
       } 

      } 
      else 
      { 
      alert(j.products + " was not added to your cart.") 
      } 
     } 

     greeting = confirm("Do you want to continue shopping? Yes or No"); 

     return selected_items; 
     } 
    } 

    // theStore() Stored array can be utilised for processing. 
    console.log(theStore()); 
+0

Fiddle: https://jsfiddle.net/a0c74pb7/ – RahulN