0

I'm trying to multiply a number from a textbox with other values through a function with parameters. For some reason I keep getting "NaN" as a result.

Here's a watered down version of the code:

function calculate(a, b, c, d) {
  var num1 = a * b;
  var num2 = num1 * a;
  document.write(num1);
  document.write(num2); 
}
<input type="number" id="apple" min="0" value="0">
<input type="button" onclick="calculate('apple','1.99','2','5')" value="Add to Order">

The document.write lines are just there for test purposes. I just don't know why num1 and num2 give back "NaN". Also, I've tried doing a.value and b.value to no avail.

5
  • 3
    If a is apple, then you try to multiply apple with 1.99. How would that become a number. Also all your numbers are strings. Remove the quotes from them. Perhaps you mean var num1 = document.getElementById(a).value*b which will multiply the value of your number with the price Commented Jun 22, 2016 at 9:43
  • 1
    Value you pass for argument is 'apple' which is a string. When u multiply a string with a number you get nothing but a 'NaN'. Commented Jun 22, 2016 at 9:43
  • a is apple in your case which is NaN Commented Jun 22, 2016 at 9:44
  • as @Oshadha mentioned and because num1 = NaN then num2 will be NaN too, because you multiple num1 which is NaN with apple which is string and then num2 will return NaN too Commented Jun 22, 2016 at 9:44
  • 'apple' * '1.99' o_O What did you expect this to do? Commented Jun 22, 2016 at 9:50

3 Answers 3

1

You are using the String 'apple' which cannot be multiplied. What you need in this case is getting the value inside the input field:

var appleVal = document.getElementById('apple').value;

or use 'a' variable instead:

var appleVal = document.getElementById(a).value;

Tip: Use parseFloat function to try to make a number of a string:

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/parseFloat

Sign up to request clarification or add additional context in comments.

Comments

0

'apple' is only text, you need to pass:

document.getElementById('apple').value.

In your code:

onclick = "calculate(document.getElementById('apple').value,'1.99','2','5')"

3 Comments

don't pass strings to represent integer values, change '1.99' to 1.99, etc.
I don't want to change the original code more than to propose a solution. But I think so is better to pass numbers as numbers. Thanks @Liam.
or just use document.getElementById(a).value in the calculate
0

Try this,

<input type="number" id = "apple" min = "0" value = "0">
<input type="button" onclick = "calculate(1.99,2,5)" value = "Add to Order">

<script>
function calculate(b,c,d){
var a = document.getElementById("apple").value;
var num1 = a*b;
var num2 = num1*a;
document.write(num1);
document.write(num2); 
}
</script>

Your mistake is, you are multiplying "apple" string with number.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.