Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

103
Visualizações
How do I prevent my equation from giving me concatenated numbers?

In this project, Ive written a script which outputs the total cost of any word you type into a textbox. The cost is $6.00 per letter.

I am also to trying to get the script to output the total cost + 7% tax. The issue is that the equation is not calculating the total properly. Instead the equation is putting the cost and tax right next to eachother instead of adding them up. So for example, if the tax is $2.1 and the total is $30, it's wrtiting it like this "$302.1" instead of like this "$32.1".

Here's a snippet of the code (the very last code in the Javascript is the one im having the issue with)

var customTitle;
var cost = 6;

function calculateCost() {
  customTitle = document.getElementById("titleBox").value;
  var titleLetters = customTitle.length;
  var spaceCount = (customTitle.split(" ").length - 1);

  document.getElementById("output1").innerHTML = customTitle;
  document.getElementById("checkout").innerHTML = "Your Order Summary";
  document.getElementById("output2").innerHTML = "Title: " + customTitle;
  document.getElementById("output3").innerHTML = "Cost: $" + (titleLetters - spaceCount) * cost;
  document.getElementById("output4").innerHTML = "Tax: $" + (titleLetters - spaceCount) * cost * .07;
  document.getElementById("output5").innerHTML = "Total: $" + (titleLetters - spaceCount) * cost * .07 + (titleLetters - spaceCount) * cost;
}
<html>

<body>

  <main>
    <p>Type your custom title below</p>
    <form>
      <input id="titleBox" type="text">
      <button onclick="calculateCost()" type="button">Generate & Checkout</button>
    </form>
  </main>
  <aside>
    <p id="output1"></p>
    <h3 id="checkout"></h3>
    <p id="output2"></p>
    <p id="output3"></p>
    <p id="output4"></p>
    <p id="output5"></p>
  </aside>

</body>

</html>

about 4 years ago · Juan Pablo Isaza
3 Respostas
Responde à pergunta

0

You just need to wrap your math in another parentheses. It is taking the string of "Total: $" + as you wanting to concatenate and will convert the output to a string pre-emptively. If you put the parentheses you get the right math and output.

var customTitle;
var cost = 6;

function calculateCost() {
  customTitle = document.getElementById("titleBox").value;
  var titleLetters = customTitle.length;
  var spaceCount = (customTitle.split(" ").length - 1);

  document.getElementById("output1").innerHTML = customTitle;
  document.getElementById("checkout").innerHTML = "Your Order Summary";
  document.getElementById("output2").innerHTML = "Title: " + customTitle;
  document.getElementById("output3").innerHTML = "Cost: $" + (titleLetters - spaceCount) * cost;
  document.getElementById("output4").innerHTML = "Tax: $" + (titleLetters - spaceCount) * cost * .07;
  document.getElementById("output5").innerHTML = "Total: $" + ((titleLetters - spaceCount) * cost * .07 + (titleLetters - spaceCount) * cost);
}
<html>

<body>

  <main>
    <p>Type your custom title below</p>
    <form>
      <input id="titleBox" type="text">
      <button onclick="calculateCost()" type="button">Generate & Checkout</button>
    </form>
  </main>
  <aside>
    <p id="output1"></p>
    <h3 id="checkout"></h3>
    <p id="output2"></p>
    <p id="output3"></p>
    <p id="output4"></p>
    <p id="output5"></p>
  </aside>

</body>

</html>

about 4 years ago · Juan Pablo Isaza Relatório

0

Another way to easily insert variables, the results of function calls, or evaluate math expressions, is by using Template literals (Template strings).

`string text ${expression} string text`

Whatever is in between ${ and } gets evaluated before its converted to a string.

var customTitle;
var cost = 6;

function calculateCost() {
  customTitle = document.getElementById("titleBox").value;
  var titleLetters = customTitle.length;
  var spaceCount = (customTitle.split(" ").length - 1);

  document.getElementById("output1").innerHTML = customTitle;
  document.getElementById("checkout").innerHTML = "Your Order Summary";
  document.getElementById("output2").innerHTML = `Title: ${customTitle}`;
  document.getElementById("output3").innerHTML = `Cost: $${(titleLetters - spaceCount) * cost}`;
  document.getElementById("output4").innerHTML = `Tax: $${(titleLetters - spaceCount) * cost * .07}`;
  document.getElementById("output5").innerHTML = `Total: $${(titleLetters - spaceCount) * cost * .07 + (titleLetters - spaceCount) * cost}`;
}
<html>

<body>

  <main>
    <p>Type your custom title below</p>
    <form>
      <input id="titleBox" type="text">
      <button onclick="calculateCost()" type="button">Generate & Checkout</button>
    </form>
  </main>
  <aside>
    <p id="output1"></p>
    <h3 id="checkout"></h3>
    <p id="output2"></p>
    <p id="output3"></p>
    <p id="output4"></p>
    <p id="output5"></p>
  </aside>

</body>

</html>

about 4 years ago · Juan Pablo Isaza Relatório

0

There are different ways to tackle this issue 1) use parseInt just like I did in the below code or you could also append a unary operator(+) to the problem you faced because; Adding strings that contain numbers does NOT add them. Instead, the strings are concatenated

var customTitle;
var cost = 6;
function calculateCost() {
  customTitle = document.getElementById("titleBox").value;
  var titleLetters = customTitle.length;
  var spaceCount = (customTitle.split(" ").length - 1);
  var totalCost = (titleLetters - spaceCount) * cost * .07 + (titleLetters - spaceCount) * cost
  document.getElementById("output1").innerHTML = customTitle;
  document.getElementById("checkout").innerHTML = "Your Order Summary";
  document.getElementById("output2").innerHTML = "Title: " + customTitle;
  document.getElementById("output3").innerHTML = "Cost: $" + (titleLetters - spaceCount) * cost;
  document.getElementById("output4").innerHTML = "Tax: $" + (titleLetters - spaceCount) * cost * .07;
  document.getElementById("output5").innerHTML = "Total: $" + totalCost;
}
<html>

<body>

  <main>
    <p>Type your custom title below</p>
    <form>
      <input id="titleBox" type="text">
      <button onclick="calculateCost()" type="button">Generate & Checkout</button>
    </form>
  </main>
  <aside>
    <p id="output1"></p>
    <h3 id="checkout"></h3>
    <p id="output2"></p>
    <p id="output3"></p>
    <p id="output4"></p>
    <p id="output5"></p>
  </aside>

</body>

</html>

about 4 years ago · Juan Pablo Isaza Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda