2016-07-20 15 views
0

このコードで私の目標は、アイテムの小さな選択から選択し、数量を選択し、合計を取得し、アイテムを削除するオプションを提供する一時的なショッピングカートを作成することです必要な場合は「チェックアウト」に進みます。私の問題は、(アラート経由で)アイテムの種類別に価格の結果を取得したら、forループで実行されているため、すべての潜在的なアラートを一緒に追加する方法がわかりません。誰にどのようにこれを行うにはアイデアを持っていますか?私はJSにはかなり新しいですし、進める方法がわかりません。これはスタック上の私の最初の質問ですので、それが奇妙な質問なら私を許してください。forループの中にある複数の警告の結果を追加する

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() 

理想的には、ユーザが「いいえ」(ライン47)私は彼らにすべての項目の合計を表示したいを求められます。

は、ここに私のコードです。

ヘルプ/ヒント/アドバイスをいただければ幸いです。

答えて

0

変数totalAmountをループの外側に宣言し、ユーザがカートに物を追加するときに追加します。その後、ユーザーがアラートをヒットしなかった場合、合計金額。

ユーザーがアイテム数を選択したときに計算される小計変数を追加しました。小計は、最後に警告される変数totalAmountに追加されます。

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

フィドル:https://jsfiddle.net/a0c74pb7/ – RahulN

関連する問題