0

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? Here is the Javascript code.

function vendingFunction() {
  console.log('Testing. 1. 2. 3. All clear!');

  var $money = "";
  var codeSequence = ["A1", "A2", "B1", "B2"];
  var $soda = ["Coca-Cola", "Fanta", "Sprite", "Schweppes"];
  const coinValue = ["0.50", "1.00"];
  const sodaPrice = ["0.55", "0.95", "1.05", "0.90"];

  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" array
    console.log(sodaPrice[0]);
  }

  if (sodaChoice == "A2") {
    window.alert("You selected Fanta.");
    window.alert("This costs $" + sodaPrice[1] + ".");
    console.log(sodaPrice[1]);
  }

  if (sodaChoice == "B1") {
    window.alert("You selected Sprite.");
    window.alert("This costs $" + sodaPrice[2] + ".");
    console.log(sodaPrice[2]);
  }

  if (sodaChoice == "B2") {
    window.alert("You selected Schweppes");
    window.alert("This costs $" + sodaPrice[3] + ".");
    console.log(sodaPrice[3]);
  }
}
Barmar
  • 741,623
  • 53
  • 500
  • 612
  • 1
    I don't understand the question. Where are you inserting anything into the vending machine? – Barmar Jun 10 '21 at 23:42
  • 1
    BTW, you should use an object whose keys are the codes, and values are object with all the details like name and price, instead of all those `if` statements. – Barmar Jun 10 '21 at 23:43

1 Answers1

0

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.

function vendingFunction() {
  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 = new Intl.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" array
            console.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();
dave
  • 62,300
  • 5
  • 72
  • 93