Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Use the algorithm for the Tip, Tax and Total problem to code a JavaScript progra

ID: 3771416 • Letter: U

Question

Use the algorithm for the Tip, Tax and Total problem to code a JavaScript program (no Java libraries!)

that will prompt for the meal and calculate and display the tip, the tax and the total.

Here is the Pseudocode:

Declare Real foodTotal

Declare Real tax

Declare Real tip

Declare Real subtotal

Declare Real grandTotal

Display “Enter food total”

Input foodTotal

Set tax = foodTotal * .07

Set subtotal = foodTotal + tax

Set tip = subtotal * .15

Set grandTotal = subtotal + tip

Display “The food total is $ ” foodTotal

Display “The tax is $ ” tax

Display “The tip is $ ” tip

Display “The total including tax and tip is $ ”

This program should show on a web browser (html) and be written in JavaScript!

Explanation / Answer

<html>
<head>

<script type="text/javascript">
function applyTax(){


var inputAmount = document.getElementById( 'dollars' ).value;
var salesTax = document.getElementById( 'tax' ).value;
var tip = document.getElementById( 'tip' ).value;
var totalAmount = (inputAmount)*(salesTax*0.01 + 1);
var tipprcnt = (inputAmount)*(tip*0.01 + 1);
var Grandtotal = parseFloat(inputAmount) + parseFloat(totalAmount) + parseFloat(tipprcnt);

document.getElementById( 'requestedGrand' ).innerHTML = Grandtotal;
}

</script>

</head>
<body>

<h1>Sales Tax + Tip Calculator</h1>
<p>Type in your price (dollar amount). Click the &quot;Calculate&quot; button to receive your total.
</p>
<p>
What is the bill amount?: $<input type="text" id="dollars" /> <br>
What is the sales tax?:<input type="text" id="tax" />%<br>
how much do you want to tip?:<input type="text" id="tip" />%
<input type="button" value="Calculate" />

</p>
<h2>The Grand Total is:</h2>
<div id="requestedAmount"> </div>
<div id="requestedTax"> </div>
<div id="requestedGrand"> </div>
</body>

</html>

thank you