0

Hello so I am trying to work on this code for my Java Script class and I am stuck on how to proceed. The instructions for the problem we were given were as follows:

Create a simple self-checkout system. Prompt for the prices and quantities of three items. Calculate the subtotal of the items. Then calculate the tax using a tax rate of 5%. Print out the line items with the quantity and total, and then print out the subtotal, tax amount, and total.

Here is my code so far:

// Make a function for a simple self-checkout system. 
// prompt the user for quantity of the items
// Prompt the user for the prices of the items

function self_Checkout () {


  var prices = [x, y, z,];
  var x = prompt('Enter value');
  var quantity_x = prompt('Enter value for quantity of item 1');
  return x * quantity_x;}
{ if 
  var y = prompt('Enter value');
  var quantity_y = prompt('Enter value for quantity of item 2');
  return y * quantity_y;
{ if
  var z = prompt('Enter value');
  var quantity_z = prompt('Enter value for quantity of item 3');
  return z * quantity_y;


  // Multiply entire total by a tax rate of 5%
  // Return value of total of all items + tax to user 
  // Use console.log or document.write?

}

Now the assignment also mentions how we are supposed to use loops objects and arrays also in this problem. I have attempted to add a array in the code. Some help on how to proceed in my code would be much appreciated, hopefully i explained it well enough to get some help.

2
  • 1
    This is not a valid JavaScript code. Besides, what is your specific problem? Commented Feb 23, 2018 at 1:58
  • We can't do your homework for you. If you have a specific problem with your code you should specify it, but just asking "what should I do next?" is too broad. Commented Feb 23, 2018 at 2:18

1 Answer 1

1

Here is working code that is all dynamic and not limited to 3 items

<!DOCTYPE html>
<html>
<head>
<script>
function getit(){
    var result = document.getElementById('demo');
        var allitems = 0;
            var itemCount = prompt("how many items do you need?");
                var items = {};
for (i = 0; i < itemCount; i++) {
items[i] = {

    name : prompt("Product Name"), 
    price : prompt("Product Price"), 
    qty : prompt("Product qty")

    }
}

for (i = 0; i < itemCount; i++) {
var subtotal = 0;
var total = 0;
subtotal = items[i].price * items[i].qty;
total = subtotal * 1.05;
allitems = allitems + subtotal; 
result.innerHTML += "Product: " + items[i].name + "<br/>";
result.innerHTML += "Total Qty: " +items[i].qty + "<br/>";
result.innerHTML += "Sub total: " + subtotal + "<br/>";
result.innerHTML += "Sub total: " + total + "<br/>";
if(i == (itemCount - 1)){result.innerHTML += "Sub total for all items: " + allitems + "<br/>";}
}
    }
</script>
    </head>
<body>
<button onclick="getit()">Shop</button>
<p id="result">Creating a JavaScript Object.</p>



</body>
</html>
Sign up to request clarification or add additional context in comments.

2 Comments

Ok so i see what your doing but the html, like im not sure what the document.getElemenById is doing in this code. So i am not familiar with html as i am just working on javascript right now and have not delved into learning much html. Can the code be done with out using html or no? Or maybe an explanation of the html parts in the code. Thanks
i don't get what you are asking? javascript has to render to HTML... plus this code is fully functioning and working solving the problem.. what is there u don't understand?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.