Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

113
Views
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 answers
Answer question

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 Report

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 Report

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 Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!