Click here to Skip to main content
15,888,610 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I am new to coding and am attempting to teach myself. I created a simple function to calculate cost * tax. I just want to get a return in a dollar amount. = $500.
What exactly did I do wrong? I'm getting NaN. I'm sure this is a simple fix.

JavaScript
function calculateTax(cost, tax) {
          console.log(`$${cost}` * `$${tax}`);       

        }
          calculateTax(1000, 0.5);


What I have tried:

Everything I could possibly think of with the very tiny amount of knowledge I have achieved thus far, lol.
Posted
Updated 1-Oct-23 8:23am
v2
Comments
Dave Kreskowiak 30-Sep-23 23:54pm    
I'll give you a hint. You're trying to multiple two text strings together, not two values.

Multiply the values together, then you can put that value in a string and return that.
Bs1818 1-Oct-23 8:50am    
that hint made it very clear, thanks for your time! that solution was very simple.

1 solution

Why do people complicate things so?
HTML
<!DOCTYPE html>
<html>
<body>
<h1>JavaScript Functions</h1>

<p>Call a function which performs a calculation and returns the result:</p>

<p id="demo"></p>

<script>
function calculateTax(cost, tax) {
    console.log(cost * tax);
    return(cost * tax);
    }



let result = calculateTax(1000, 0.5);
document.getElementById("demo").innerHTML = result;
</script>

</body>
</html>
(Sandbox from w3schools: W3Schools Tryit Editor[^] )
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900