-4

I have this javascript code, and it is supposed to solve for x and y, but the result it gives is NaN.

function calculate(x,y){
                var firsttrans="1";
                var secondtrans="2";
                var a=parseInt(firsttrans, 10);
                var b=parseInt(secondtrans, 10);
                var y=x*a/b;
                var ans=parseInt(y, 10);
                alert(ans);
            }

I need firsttrans and secondtrans to be strings because of something else in my code, but the point is that this alert(ans)doesn't give a number.

VC.One
  • 14,790
  • 4
  • 25
  • 57
  • 8
    What is `x`? Why do you redeclare `y`? – str Jul 20 '17 at 18:05
  • 2
    what inputs are using? – aalcutt Jul 20 '17 at 18:05
  • 3
    Why are you defining `var y` if it is a parameter already? – Surreal Jul 20 '17 at 18:05
  • You have `y` declared twice as function argument and local variable. I've tested your function and it gives the correct result if you pass x as a number. – lilezek Jul 20 '17 at 18:06
  • **And** note that as `y` is the result of a numeric operation, using `parseInt` on it makes little sense. `parseInt` is for parsing *strings*. If you use it on a number, the number is converted to string, and then parsed as an integer. If you want to truncate the fractional portion, use `Math.floor` or `Math.ceil` or `Math.round` (as dictated by your requirements). – T.J. Crowder Jul 20 '17 at 18:07
  • 1
    @lilezek Actually `y` is not redeclared, the declaration of _predeclared_ variables is ignored. `y` is re-assigned only. – Teemu Jul 20 '17 at 18:08
  • 1
    @Surreal: The `var` before `y` is definitely wrong (in that it's misleading to anyone coming to the code), but ends up being a no-op. More significantly is that `y` is overwritten before its passed-in value is used for anything. – T.J. Crowder Jul 20 '17 at 18:08
  • What are the values of `x` and `y`? – anonymous Jul 20 '17 at 18:09
  • 3
    Post a *complete* program that demonstrates the problem. Don't make us guess what the value of `x` is. Short answer: if x isn't a number(ish) variable it'll say NaN. – aquinas Jul 20 '17 at 18:10
  • Welcome to Stack Overflow! Please take the [tour], have a look around, and read through the [help], in particular [*How do I ask a good question?*](/help/how-to-ask) Note that for JavaScript (and/or HTML, and/or CSS) questions, you can include a **runnable** [mcve] using Stack Snippets (the `[<>]` toolbar button) demonstrating the problem, which makes it easier for people to see the problem and show you the solution. – T.J. Crowder Jul 20 '17 at 18:14

2 Answers2

0

There is not guarantee that your x and y are integers or floats, so your operation var y=x*a/b; will fail. You need to make sure that x and y are integers or float.

Navneet Goel
  • 134
  • 4
0

Your code works fine (tested in Chrome). I tried calculate(8,3); and the answer was 4 not NaN so either you're trolling or else you're not telling us an important detail to recreate the NaN issue...

All your code does is simply take x value and halves it, then sets that halved result as new value of y. Why would a multiply by own half give a NaN error? I mean that y = x * a/b which does same thing as simply y = x/2...

Test this HTML code in your usual browser then against Chrome :

<!DOCTYPE html>
<html>
<body>

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

<script>
function calculate(x,y){
                var firsttrans="1";
                var secondtrans="2";
                var a=parseInt(firsttrans, 10);
                var b=parseInt(secondtrans, 10);
                var y=x*a/b;
                var ans=parseInt(y, 10);
                //alert("result is : " + ans);

                return ans;
            }

document.getElementById("demo").innerHTML = "result is : " + calculate(8,3);
</script>

</body>
</html>
VC.One
  • 14,790
  • 4
  • 25
  • 57