Doubt with javascript college work using DOM

Asked

Viewed 310 times

-4

Good morning, guys, I have this job to do and deliver tomorrow at the junior college, but I’m completely lost on how to work this out at Notepad++. Could you please give me a light? I’ve been trying since 2:00 in the morning and I can’t. Thank you in advance.

Create an HTML form with javascript with the following fields:

  • Product value(text field); quantity(text field)

  • The program must calculate the total cost (quantity x product value);

  • The program should calculate the profit on each product (value of product x 65%);

  • The program should calculate the total profit (profit x quantity);

  • The program should calculate taxes on each product (product value + profit x 18.7%);

  • The program should calculate the total tax (taxes * quantity);

  • The program should calculate the selling price (value of the product + profit + taxes);

  • Print all calculations within a paragraph (< p >)

<title>Trabalho</title>
<script>

function calcularImposto(){
resultado = document.getElementById("resultado");
n1 = document.getElementById("n1").value;
imposto1 = parseFloat(n1)*0.22;
imposto2 = parseFloat(n1)*0.18;

resultado.innerHTML = "imposto 1" + imposto1 + "<br /> Imposto 2 " + imposto2;
}
</script>
</head>
<body>
    <form>
    <label>Número 1:</label>
    <input type="text" name="n1" id="n1" size="5" />
    <br />

    <input type="button" name="botao" id="botao" value="Calcular"  onclick="calcularSoma()" />
</form>

<p>Resultado: <span id="resultado"> 0 </span></p> 
</body>

This is the code that the teacher passed as an example to help in the work itself. My question is on how to adapt this code for the exercises. I thought I’d do it with some variables like this:

var quantidade, produto
var lucro, imposto
var totalCusto, totalImposto, totalLucro
var precoVenda

custo total = quantidade * produto
lucro = (produto / 100) * 65
imposto = ((produto + lucro) / 100) * 18,7
totalLucro = lucro * quantidade
totalImposto = imposto * quantidade
precoVenda = produto + lucro + imposto

But I don’t know if it would work. I missed a few classes because of work and now I’m kind of desperate.

  • As for javascript, it’s to use jQuery, DOM, some other API, or whatever you want?

  • Victor, I must use the DOM.

1 answer

1


First you have to read the product and quantity values:

var produto = document.getElementById("produto").value;
var quantidade = document.getElementById("quantidade").value;

You will need to have these fields in your HTML:

<input type="text" id="produto" value="" />
<input type="text" id="quantidade" value="" />

At the end of your javascript, you concatenate everything into a very large string and place it inside the tag <p> thus:

document.getElementById("resultado").innerHTML = suaString;

Also, in HTML you are using onclick="calcularSoma()", but in javascript its function is calcularImposto().

There is one more problem as well. You are calculating the tax variable with a number 18,7. Should be 18.7. It’s a dot, not a comma.


And please, next time put a better title to the question. Titles like "please help me" tend to irritate community members and make them less likely to help.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.