JavaScript:当函数传入参数时,它返回```NaN``` [英] JavaScript : When function passing in the argument it is returning ```NaN```

查看:66
本文介绍了JavaScript:当函数传入参数时,它返回```NaN```的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我的代码问题

function abc(num,x,y){
 return num * x + " " + num * y;
}

abc(5,2,4); //returns 10 20

abc(5,abc(5,6,6),4) //doesn't return 30 30 20 

我还想知道如何调用下面的函数(该函数的参数内部的函数)-

I also want to know how to achieve if I call the function below (function inside the argument of the same function)-

abc(5,abc(abc(5,abc(5,6,6),abc(5,abc(5,6,6),4)))) // it must return something like how abc(5,abc(5,6,6),4) should return 30 30 20

我希望在我调用函数 abc(5,2,abc(5,abc(5,4,4),3))时应每次返回所有参数,例如 1020 20 15 调用函数- abc(5,2,abc(5,abc(5,4,4),3))如果我在函数中放置了相同的函数参数/参数.

I want when I call the function abc(5,2,abc(5,abc(5,4,4),3)) should return all parameter every time like 10 20 20 15 for calling the function - abc(5,2,abc(5,abc(5,4,4),3)) if I place the same function in the function Parameter/arguments.

我尝试过,但是当我在相同的函数参数中调用函数时,例如 abc(5,abc(5,6,6),4)中的 x 函数abc(num,x,y)变为 NaN ,因此返回类似于 NaN 20 而不是 30 30 20

I tried but when I call the function inside the same function arguments like abc(5,abc(5,6,6),4) the x in the function abc(num,x,y) becomes NaN hence returns like NaN 20 but not 30 30 20

如果我喜欢

 function abc(num,x,y){
if(isNaN(x)){
return x + " " + y * num ;  //now it returns 30 30 20 for abc(5,abc(5,6,6),4)
}
 return num * x + " " + num * y;
}

但是如何为此 abc(5,abc(abc(abc,5,abc(5,6,6),abc(5,abc(5,6,6),4))))返回与上面编辑的方式相同的方式.

but how to do for this abc(5,abc(abc(5,abc(5,6,6),abc(5,abc(5,6,6),4)))) to return the same way like above edited.

推荐答案

您的函数abc返回一个字符串类型,您正尝试将其与一个数字相乘,这将导致NaN,并在调用abc时尝试使用parseInt对其进行强制转换应该很好.

Your function abc returns a string type which you are trying to multiply it with a number which will result into NaN, trying casting it with parseInt while calling abc and it should be fine.

function abc(num,x,y){
     return num * x + " " + num * y;
}
    
abc(5,2,4); //returns 10 20
    
abc(5,parseInt(abc(5,6,6)),4) // return "150 20"

这篇关于JavaScript:当函数传入参数时,它返回```NaN```的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆