Javascript discount for price classes - javascript

Ive been trying to add a discount to price classes for a couple of days now but haven’t been able to. The current js code I have is a simple addition calculator, but I want it to discount prices between 100 and 500 with 10% and prices over 500 get 20% discount. I also want it to show the price before and after the discount if possible.
The code I have for the calculator so far, its working fine:
function calculate() {
var field1 = document.getElementById("num1").value;
var field2 = document.getElementById("num2").value;
var result = parseFloat(field1) + parseFloat(field2);
if (!isNaN(result)) {
document.getElementById("answer").innerHTML = "Totalpris är " + result;
}
}
Artikel 1 <input type="text2" id="num1">
<br>
<br> Artikel 2 <input type="text2" id="num2">
<br>
<br>
<button onclick="calculate()">Totalpris</button>
<h1 id="answer"></h1>

This is pretty simple to do with some if statements.
function calculate() {
var field1 = document.getElementById("num1").value;
var field2 = document.getElementById("num2").value;
var beforeDiscount = parseFloat(field1) + parseFloat(field2);
var afterDiscount = beforeDiscount;
if (beforeDiscount >= 100 && beforeDiscount < 500) {
afterDiscount = beforeDiscount * 0.9;
} else if (beforeDiscount >= 500) {
afterDiscount = beforeDiscount * 0.8;
}
if (!isNaN(beforeDiscount)) {
document.getElementById("answer").innerHTML =
"Totalpris är "+afterDiscount+". Was "+beforeDiscount;
}
}
Artikel 1 <input type="text2" id="num1">
<br>
<br>
Artikel 2 <input type="text2" id="num2">
<br>
<br>
<button onclick="calculate()">Totalpris</button>
<h1 id="answer"></h1>

You should give type number
text2 is not a valid value for the type attribute
Instead of this
Artikel 1 <input type="text2" id="num1">
<br>
<br>
Artikel 2 <input type="text2" id="num2">
Do this
<input type ="number" id="num1">
<input type="number" id="num2">
Here is the solution for your challenge
<!DOCTYPE html>
<html>
<head>
</head>
<body>
a.htmlArtikel 1 <input id="num1" type="number">
<br>
<br>
Artikel 2 <input type="number" id="num2">
<br>
<br>
<button onclick="calculate()">Totalpris</button>
<script>
function calculate(){
let result = ``;
const field1 = document.getElementById("num1").value;
const field2 = document.getElementById("num2").value;
let amount_before_discount = parseFloat(field1)+parseFloat(field2);
let amount_after_discount = amount_before_discount
if(amount_after_discount >= 100 && amount_before_discount < 500){
amount_after_discount = amount_before_discount * 0.9;
// for the second question, look at the comments
result += `After an discount of 10% the new price is
${amount_after_discount}`
}else if(amount_before_discount >= 500){
amount_after_discount = amount_before_discount * 0.8;
result += `After an discount of 20% the new price is
${amount_after_discount}`
}
if (!isNaN(amount_before_discount)) {
// here you can innerHtml then the result
document.getElementById("answer").innerHTML =
"Totalpris är "+amount_after_discount+". Was "+amount_before_discount;
}
}
</script>
<h1 id="answer"></h1>
</body>
</html>

Related

Writing a code in javascript with radio buttons and using if else loop to calculate simple and/or compound interest

I have been asked to write a code in javascript to show the results in the output boxes based on the input data, the form should like the one shown in the attached image:Interest Calculator
Till now, I have written down the following code; however I am unable to get the results in the output boxes. Could someone please help. Thanks in advance.
function calc() {
{
var p = document.getElementById("p").value;
var r = document.getElementById("r").value;
var t = document.getElementById("t").value;
var int = f.int.value;
}
if (int === "si") {
var sip = (p * r * t) / 100
var ta = p + sip
document.getElementById("i").innerHTML = sip;
document.getElementById("a").innerHTML = ta;
} else {
var cta = p * (Math.pow((1 + r / 100), t))
var cmp = cta - p
document.getElementById("i").innerHTML = cmp;
document.getElementById("a").innerHTML = cta;
}
}
<form name="f">
<h1> Interest Calculator </h1>
Principal = <input type="text" id="p" autofocus>
<br><br><br> Rate of Interest = <input type="text" id="r">
<br><br><br> Time (in years) = <input type="text" id="t">
<br><br>
<h1> Interest Type </h1>
<input type="radio" name="int" id="int" value="si"> Simple Interest
<input type="radio" name="int" id="int" value="ci"> Compound Interest
<br><br>
<hr noshade> Interest <input type="text" id="i">
<br><br> Amount <input type="text" id="a">
<br><br>
<input type="button" name="cal" value="Calculate" onclick="calc()">
<input type="reset" value="Reset">
</form>
innerHTML is used to retrieve or update the content/markup inside a DOM node that can have children. For input elements, you have to use value to get or set the value of the field.
function calc() {
{
var p = document.getElementById("p").value;
var r = document.getElementById("r").value;
var t = document.getElementById("t").value;
var int = f.int.value;
}
if (int === "si") {
console.log("simple interest");
var sip = (p * r * t) / 100
var ta = p + sip
document.getElementById("i").value = sip;
document.getElementById("a").value = ta;
} else {
console.log("compound interest");
var cta = p * (Math.pow((1 + r / 100), t))
var cmp = cta - p
document.getElementById("i").value = cmp;
document.getElementById("a").value = cta;
}
}
<form name="f">
<h1> Interest Calculator </h1>
Principal = <input type="text" id="p" autofocus>
<br><br><br> Rate of Interest = <input type="text" id="r">
<br><br><br> Time (in years) = <input type="text" id="t">
<br><br>
<h1> Interest Type </h1>
<input type="radio" name="int" id="int" value="si"> Simple Interest
<input type="radio" name="int" id="int" value="ci"> Compound Interest
<br><br>
<hr noshade> Interest <input type="text" id="i">
<br><br> Amount <input type="text" id="a">
<br><br>
<input type="button" name="cal" value="Calculate" onclick="calc()">
<input type="reset" value="Reset"><br><br><br>
</form>
Replace innerHTML with value. Because input values are updated using value attribute. Also
the braces here
{
var p = document.getElementById("p").value;
var r = document.getElementById("r").value;
var t = document.getElementById("t").value;
var int = f.int.value;
}
is not required.
function calc() {
var p = document.getElementById("p").value;
var r = document.getElementById("r").value;
var t = document.getElementById("t").value;
var int = f.int.value;
if (int === "si") {
var sip = (p * r * t) / 100
var ta = p + sip
document.getElementById("i").value = sip;
document.getElementById("a").value = ta;
} else {
var cta = p * (Math.pow((1 + r / 100), t))
var cmp = cta - p
document.getElementById("i").value = cmp;
document.getElementById("a").value = cta;
}
}
<form name="f">
<h1> Interest Calculator </h1>
Principal = <input type="text" id="p" autofocus>
<br><br><br> Rate of Interest = <input type="text" id="r">
<br><br><br> Time (in years) = <input type="text" id="t">
<br><br>
<h1> Interest Type </h1>
<input type="radio" name="int" id="int" value="si"> Simple Interest
<input type="radio" name="int" id="int" value="ci"> Compound Interest
<br><br>
<hr noshade> Interest <input type="text" id="i">
<br><br> Amount <input type="text" id="a">
<br><br>
<input type="button" name="cal" value="Calculate" onclick="calc()">
<input type="reset" value="Reset">
</form>

adding multiplied numbers using javascript

function multiplyBy()
{
num1 = document.getElementById("firstNumber").value;
num2 = document.getElementById("secondNumber").value;
num3= document.getElementById("result").value = num1 * num2;
document.getElementById("total").value = +num3 ;
}
1st Number : <input type="text" id="firstNumber" value="" /><br>
2nd Number: <input type="text" id="secondNumber" value="" onchange="multiplyBy()" /><br>
<p>The Result is : <br>
<input type="text" name="result" id = "result" value=""/>
</p>
<p>Total :<br>
<input type="text" name="total" id="total" value=""/>
</p>
function multiplyBy()
{
num1 = document.getElementById("firstNumber").value;
num2 = document.getElementById("secondNumber").value;
num3= document.getElementById("result").value = num1 * num2;
}
1st Number : <input type="text" id="firstNumber" value="" /><br>
2nd Number: <input type="text" id="secondNumber" value=""
onchange="multiplyBy()" /><br>
<p>The Result is : <br>
<input type="text" name="result" id = "result" value=""/>
</p>
I am multiplying two numbers here.how to add those multiplied numbers in another text box.suppose 2&5 are multiplied later 4&6 multiplied how to add those numbers.
Try this. You need to parse the input value using parseFloat. This will convert the string to a number. The existing result value is added to the newly calculating value. |0 is used for the false value of input getting 0
Updated
prev, present and total result box were added
Append each additional input with new result.
var last=0;
function multiplyBy() {
var num1 = document.getElementById("firstNumber");
var num2 = document.getElementById("secondNumber");
var prev = document.getElementById("prev");
var present = document.getElementById("present");
var total = document.getElementById("result");
prev.value=last;
present.value=(parseFloat(num1.value) * parseFloat(num2.value))
last = last+(parseFloat(num1.value) * parseFloat(num2.value))
total.value=last;
num1.value = "";
num2.value = "";
}
1st Number : <input type="text" id="firstNumber" value="" /><br> 2nd Number: <input type="text" id="secondNumber" value="" onchange="multiplyBy()" /><br>
<p >
Prev result:<br>
<input type="text" name="result" id="prev" ><br>
Present:<br>
<input type="text" name="result" id="present" ><br>
The total Result is : <br>
<input type="text" name="result" id="result"><br>
</p>
You just need for get value of text box and add it with result
function multiplyBy()
{ var sum = 0;
var num=0;
num1 = document.getElementById("firstNumber").value;
num2 = document.getElementById("secondNumber").value;
num =parseInt(num1 * num2);
num3=parseInt(document.getElementById("result").value);
document.getElementById("result").value=(num + num3);
}
</script>
1st Number : <input type="text" id="firstNumber" value="" /><br>
2nd Number: <input type="text" id="secondNumber" value=""
onchange="multiplyBy()" /><br>
<p>The Result is : <br>
<input type="text" name="result" id = "result" value="0"/>
</p>
I have added the multiplyBy() to both the input fields and changed the type to number to restrict the user to enter number only.
Please check the code snippet below.
document.getElementById("firstNumber").addEventListener("change",multiplyBy);
document.getElementById("secondNumber").addEventListener("change",multiplyBy);
function multiplyBy()
{
actualResult = document.getElementById("actualResult").value ? document.getElementById("actualResult").value : 0; // Return zero if there is no value in actualResult input field
num1 = document.getElementById("firstNumber").value;
num2 = document.getElementById("secondNumber").value;
num3= document.getElementById("result").value = num1 * num2;
document.getElementById("actualResult").value = Number(actualResult) + num3;
}
1st Number : <input type="number" id="firstNumber" value=""/><br>
2nd Number: <input type="number" id="secondNumber" value=""/><br>
<p>The Current Result is : <br>
<input type="number" name="result" id = "result" value=""/>
</p>
<p>Previous Result + Current Result : <br>
<input type="number" name="actualResult" id = "actualResult" value=""/>
</p>
You need to add event handlers if you want to calculate on change of textbox input like following.
function multiplyBy()
{
var result = document.getElementById("result");
var num1 = document.getElementById("firstNumber").value;
var num2 = document.getElementById("secondNumber").value;
var num3= parseFloat(num1) * parseFloat(num2);// to convert entered values to float and || 0 to use 0 if no value is there
if(!isNaN(num3)){
document.getElementById("resultPrev").value = document.getElementById("result").value;
document.getElementById("resultCurrent").value = num3;
result.value = parseFloat(result.value || 0) + num3;
}
}
document.getElementById("firstNumber").addEventListener("change",multiplyBy);
document.getElementById("secondNumber").addEventListener("change",multiplyBy);
1st Number : <input type="text" id="firstNumber" value="" /><br>
2nd Number: <input type="text" id="secondNumber" /><br>
<p>The Previous Result is : <br>
<input type="text" name="resultPrev" id = "resultPrev" value="0"/>
</p>
<p>The Current Result is : <br>
<input type="text" name="resultCurrent" id = "resultCurrent" value=""/>
</p>
<p>The Final Result is : <br>
<input type="text" name="result" id = "result" value=""/>
</p>
Consider handling non number values.

Lottery game in javascript

The user should enter five numbers and then after clicking the button another five numbers should be extracted randomly. Based on how many of the numbers guessed match with the random numbers, the program should print six different sentences.
The problem is that I'm stuck, I don't know to to fix it
<!doctype html>
<html lang=it>
<head>
<meta charset="utf-8">
<title>SUPERENALOTTO</title>
<script type="text/javascript">
function reset()
{
document.getElementById("numero1")"
document.getElementById("numero2")"
document.getElementById("numero3")"
document.getElementById("numero4")"
document.getElementById("numero5")"
}
function guessnumbers()
{
var num1,num2,num3,num4,num5,ran1,ran2,ran3,ran4,ran5,N,monete,listanum,ris1,ris2,ris3,ris4,ris5,tot,vincita;
num1=document.getElementById("num1").value;
num2=document.getElementById("num2").value;
num3=document.getElementById("num3").value;
num4=document.getElementById("num4").value;
num5=document.getElementById("num5").value;
monete=document.getElementById("monete").value;
ran1=Math.floor((Math.random()*50));
ran2=Math.floor((Math.random()*49));
ran3=Math.floor((Math.random()*48));
ran4=Math.floor((Math.random()*47));
ran5=Math.floor((Math.random()*46));
N=50;
listanum.apply(null, {length: N}).map(Number.call, Number)
if ((listanum[ran1]==num1)||(listanum[ran2]==num1)||(listanum[ran3]==num1)||(lista[ran4]==num1)||(lista[ran5]==num1))
{
ris1=1;
}
else ris1=0;
if ((listanum[ran1]==num2)||(listanum[ran2]==num2)||(listanum[ran3]==num2)||(lista[ran4]==num2)||(lista[ran5]==num2))
{
ris2=1;
}
else ris2=0;
if ((listanum[ran1]==num3)||(listanum[ran2]==num3)||(listanum[ran3]==num3)||(lista[ran4]==num3)||(lista[ran5]==num3))
{
ris3=1;
}
else ris3=0;
if ((listanum[ran1]==num4)||(listanum[ran2]==num4)||(listanum[ran3]==num4)||(lista[ran4]==num4)||(lista[ran5]==num4))
{
ris4=1;
}
else ris4=0;
if ((listanum[ran1]==num5)||(listanum[ran2]==num5)||(listanum[ran3]==num5)||(lista[ran4]==num5)||(lista[ran5]==num5))
{
ris5=1;
}
else ris5=0;
tot= ris1+ris2+ris3+ris4+ris5
if (tot==5)
{
vincita=(monete*20);
document.getElementById("result").innerHTML=" CINQUINA: Congratulazioni! Vinci "+vincita;
}
if (tot==4)
{
vincita=(monete*10);
document.getElementById("result").innerHTML=" Quaterna: Congratulazioni! Vinci "+vincita;
}
if (tot==3)
{
vincita=(monete*5);
document.getElementById("result").innerHTML=" Terna: Congratulazioni! Vinci "+vincita;
}
if (tot==2)
{
vincita=(monete*2);
document.getElementById("result").innerHTML=" Ambo: Vinci "+vincita;
}
if (tot==1)
{
vincita=(monete*1);
document.getElementById("result").innerHTML=" Un solo numero indovinato: Non perdi e non guadagni ";
}
if (tot==0)
{
vincita=(monete*0);
document.getElementById("result").innerHTML=" Mi dispiace: perdi tutto";
}
}
</script>
</head>
<body>
<div id="titolo">
<h1 id="myDIV">superenalotto</h1>
</div>
<div id="informazione">
</div>
<div id="gioco">
<form>
<p style="text-align:center; font-size:30px ">
<br>
1°Numero <input type="text" id="num1" name="num1">
<br><br>
2°Numero <input type="text" id="num2" name="num2">
<br><br>
3°Numero <input type="text" id="num3" name="num3">
<br><br>
4°Numero <input type="text" id="num4" name="num4">
<br><br>
5°Numero <input type="text" id="num5" name="num5">
<br><br>
Numero pescato1 <input type="text" id="num6" name="num6">
<br><br>
Numero pescato2 <input type="text" id="num7" name="num7">
<br><br>
Numero pescato3 <input type="text" id="num8" name="num8">
<br><br>
Numero pescato4 <input type="text" id="num9" name="num9">
<br><br>
Numero pescato5 <input type="text" id="num10" name="num10">
<br><br>
<h2 style="font-size: 30px; "> Inserisci la tua scommessa</h2>
<br>
<input type="text" id="monete" name="monete">
<br><br>
<input type="button" id="submit" name="Gioca" value="Gioca" onclick="guessnumbers()">
<br><br>
<span id="result" style="font-size:25px; color:red"> </span>
<br>
<br>
<br><br><br>
<input type="submit" id="submit" name="cancella" style=" background- color:red; width:160px; height:50px" value="Ritenta" onclick="reset()">
</form>
</div>
<br><br><br><br><br>
</body>
</html>
var howManyNumbers = 5;
var rememberNumbersArray = []; // We do not repeat numbers
createInput("userInputId", false, "Type number from 1 to 10");
createInput("inputId", true);
// create 6 (howManyNumbers) inputs
function createInput(name, disabled, placeholder){
for(var e = 0; e<howManyNumbers; e++){
lottery.innerHTML+=e+". ";
var input = document.createElement("input");
input.type = "text";
input.id = name+e; // id
if(placeholder){
input.placeholder = placeholder;
}
input.disabled = disabled;
lottery.appendChild(input); // put it into the DOM
lottery.innerHTML+="<BR><BR>"; // gap :D you can use
}
}
function randomNumbers(){
// clear rememberNumbersArray for next lottery :D
rememberNumbersArray = [];
// clear results
results.innerHTML = "";
var count = howManyNumbers;
var numberRnd = 0;
var rndNmb = 0;
var count = 0;
while(count<howManyNumbers){
// ...random
rndNmb = mt_rand(10,1);
if(rememberNumbersArray.indexOf(rndNmb)==-1){
// update input
document.getElementById("inputId"+count).value = rndNmb;
// Remember a random number
rememberNumbersArray.push(rndNmb);
// if count == howManyNumbers, break the loop
count++;
}
}
// check if somebody win
checkWin(count);
}
function checkWin(id){
results.innerHTML = "Winning numbers: <BR>";
for(var e = 0; e<rememberNumbersArray.length; e++){
for(var a = 0; a<howManyNumbers; a++){
if(rememberNumbersArray[e]==document.getElementById("userInputId"+a).value){
results.innerHTML += rememberNumbersArray[e]+"<BR>";
}
}
}
}
function mt_rand(max, min){
return Math.floor(Math.random() * max) + min;
}
Welcome to ITALIA SUPER LOTTO 2017<BR><BR>
<div id="lottery"></div>
<button onclick="randomNumbers();">Generate</button>
<div id="results"></div>

Failure to calculate the total monthly payment for a car loan. What's going wrong?

Updated code. calculate() still not working. The monthly payment amount is ultimately not being passed to the "total" id box. Does anyone see what the underlying problem is here? My syntax seems to be correct, I believe there may be a problem with my specification of where each variable is applied in the code.
I am having problems getting the function calculate() to pass the correct result in "total." Any possible solutions? The action of clicking the calculate button should display the total, but is displaying nothing at all, as if the button does not activate the calculate function at all.
<!DOCTYPE html>
<html>
<head>
<script type="text/javascript">
function calculate() {
var P = document.getElementById("price").value;
var D = document.getElementById("DP").value;
var R = document.getElementById("R").value;
var N = document.getElementById("N").value;
var i = (R / 1200);
var n = (N * 12);
var m = ((P - D) * i * Math.pow(1 + i,n)) / (Math.pow(1 + i,n) - 1);
var result = document.getElementById('total');
result.value = m;}
</script>
</head>
<div align="center">
<hr>
<form name id="Main">
<input type="number" id="price" placeholder="Price of the Car"/>
<br>
<br>
<input type="number" id="DP" placeholder="Down Payment"/>
<br>
<br>
<input type="number" id="R" placeholder="Annual % Rate"/>
<br>
<br>
<input type="number" id="N" placeholder="# of Years Loaned"/>
<br>
<br>
<input type="button" id="calculate" value="Calculate" onclick="javascript:calculate();"/>
<br>
<br>
<input type="number" id="total" placeholder="Total Cost..." readonly=""/>
<br>
<br>
<input type="reset" value="Reset">
</form>
<hr>
</div>
</html>
Use Math.pow() function instead. The ^ operator is not for mathematical power operations (see https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Bitwise_Operators#Bitwise_XOR and https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Math/pow).
Also, It's result.value = m, not the other way around :)
Also 2: R and M seem undefined to me, you have to initialize those variables with something, like you did with P and D.
Also 3: use chrome dev tools or anything like that. It will make your life much easier. Remember, javascript doesn't mean "no IDE" :D
Unfortunately, your original code will never work, as it has too many errors.
Here is the corrected fiddle for you, please, have a look:
http://jsfiddle.net/q330fqw8/
function calculate() {
var P = document.getElementById("price").value;
var D = document.getElementById("DP").value;
var R = document.getElementById("R").value;
var N = document.getElementById("N").value;
var i = (R / 1200);
var n = (N * 12);
var m = ((P - D) * i * Math.pow(1 + i,n)) / (Math.pow(1 + i,n) - 1);
var result = document.getElementById('total');
result.value = m;
}
I removed id="calculate" in HTML, because of this: Why JS function name conflicts with element ID?
In Javascript, the 4 variables P,D,R,N should be set properly. Finally, this m.value = result; -> result.value = m;
but I guess, you've already corrected some errors in the question. I worked with your original code.
<!DOCTYPE html>
<html>
<head>
<script type="text/javascript">
function calculate() {
var P = document.getElementById("price").value;
var D = document.getElementById("DP").value;
var R = document.getElementById("R").value;
var N = document.getElementById("N").value;
var i = (R / 1200);
var n = (N * 12);
var m = ((P - D) * i * Math.pow(1 + i,n)) / (Math.pow(1 + i,n) - 1);
var result = document.getElementById('total');
result.value = m;}
</script>
</head>
<div align="center">
<hr/>
<form id="Main">
<input type="number" id="price" placeholder="Price of the Car" />
<br />
<br/>
<input type="number" id="DP" placeholder="Down Payment" />
<br/>
<br/>
<input type="number" id="R" placeholder="Annual % Rate" />
<br/>
<br/>
<input type="number" id="N" placeholder="# of Years Loaned" />
<br/>
<br/>
<input type="button" value="Calculate Monthly Payment" onclick="calculate();" />
<br/>
<br/>
Total Monthly Payment:<input type="number" id="total" placeholder="Total Cost..." readonly="" />
<br/>
<br/>
<input type="reset" value="Reset" />
</form>
<hr/>
</div>

Calculator implemented in JavaScript

I've been trying to figure out what is wrong with my code, I need to add the products (bananas, sodas, chips, candy) then multiply it with 10% tax given. Am I missing the variables for those products? I know something is missing but I don't know what to do!
<html>
<head>
<title>Total Calculator</title>
</head>
<body>
<p>
Bananas: <input type="text" id="bananasBox" value="" /> at $ 0.50 a piece<br/>
Sodas: <input type="text" id="sodasBox" value="" /> at $ 0.75 per can<br/>
Chips: <input type="text" id="chipsBox" value="" /> at $1.25 per bag<br/>
Candy: <input type="text" id="candyBox" value="" /> at $1.00 per pack<br/>
TAX is 10 %
</p>
<button id="Calculate" onclick= "Calculate()" value="Calculate">Calculate</button>
<script>
function Calculate(){
var total = 0;
var cost = document.getElementById("cost").value;
var tax = document.getElementById("tax").value;
total = cost * tax;
document.getElementById("total").value = total;
document.getElementById("outputDiv").innerHTML= "Your TOTAL is: " + total;
}
</script>
<hr/>
<div id="outputDiv">
</div>
</body>
</html>
I don't think you really put forth any effort, but here's a correct answer:
<html>
<head>
<title> Total Calculator </title>
</head>
<body>
<p>
Bananas: <input type="text" id="bananasBox" value=""> at $ 0.50 a piece<br>
Sodas : <input type="text" id="sodasBox" value=""> at $ 0.75 per can<br>
Chips : <input type="text" id="chipsBox" value=""> at $1.25 per bag<br>
Candy : <input type="text" id="candyBox" value=""> at $1.00 per pack<br>
TAX is 10 %
</p>
<input type="button" value="Calculate" id='Calculate' onclick= "Calculate()">
<script type="text/javascript">
function Calculate() {
var total = 0;
var bananas = Number(document.getElementById("bananasBox").value) * .5;
var sodas = Number(document.getElementById("sodasBox").value) * .75;
var chips = Number(document.getElementById("chipsBox").value) * 1.25;
var candy = Number(document.getElementById("candyBox").value) * 1;
var tax = 0.10;
total = (bananas+sodas+chips+candy)*(1+tax);
document.getElementById('outputDiv').innerHTML= 'Your TOTAL is: $' + Number(total).toFixed(2);
}
</script>
<hr>
<div id="outputDiv">
</div>
</body>
</html>
If you want me to explain it I will, but if you don't really have any intention in learning: it wouldn't be worth wasting my time.

Categories

Resources