Skip to content Skip to sidebar Skip to footer

How Do You Program This Javascript File For Allowing More Than One Denominations To Be Inserted Into A Vending Machine?

I am working on a school assignment where I must have the code to allow more than one denomation inserted into the vending machine and checks if you can buy four soda products? Her

Solution 1:

Add a function to insert money, ask what type of coin, and then lookup how much that coin is worth, and track how much they have inserted. Then, when they buy, check if they have enough money for that item.

functionvendingFunction() {
  var $money = 0;
  var codeSequence = ["A1", "A2", "B1", "B2"];
  var $soda = ["Coca-Cola", "Fanta", "Sprite", "Schweppes"];
  const coinValue = { quarter: .25, nickel: .05, dime: .10, penny: .01 };
  const sodaPrice = [0.55, 0.95, 1.05, 0.90];
  const formatMoney = newIntl.NumberFormat('en-US', {
    style: 'currency',
    currency: 'USD',
  }).format;


  return {
      insertCoin: () => {
          var coin = window.prompt("Insert a quarter, dime, nickel, or penny.");
          $money += coinValue[coin] || 0;
          if (typeof coinValue[coin] === 'undefined') alert('Invalid Choice');
          console.log('You now have ' + formatMoney($money));
      },
      selectItem: () => {
          var sodaChoice = window.prompt("Select your code.");
          console.log(sodaChoice);

          if (sodaChoice == "A1") {
            window.alert("You selected Coca-Cola.");
            window.alert("This costs $" + sodaPrice[0] + "."); //sodaprice[0] calls the first element in "sodaPrice's" arrayconsole.log(sodaPrice[0] <= $money ? "You have enough" : "You don't have enough");
          }

          if (sodaChoice == "A2") {
            window.alert("You selected Fanta.");
            window.alert("This costs $" + sodaPrice[1] + ".");
            console.log(sodaPrice[1] <= $money ? "You have enough" : "You don't have enough");
          }

          if (sodaChoice == "B1") {
            window.alert("You selected Sprite.");
            window.alert("This costs $" + sodaPrice[2] + ".");
            console.log(sodaPrice[2] <= $money ? "You have enough" : "You don't have enough");
          }

          if (sodaChoice == "B2") {
            window.alert("You selected Schweppes");
            window.alert("This costs $" + sodaPrice[3] + ".");
            console.log(sodaPrice[3] <= $money ? "You have enough" : "You don't have enough");
          }
      }
  }
}

const app = vendingFunction();
for (let i = 0; i < 4; i++) {
    app.insertCoin();
}
app.selectItem();

Post a Comment for "How Do You Program This Javascript File For Allowing More Than One Denominations To Be Inserted Into A Vending Machine?"