简体   繁体   English

JavaScript:当 function 传入参数时,它返回 ``NaN```

[英]JavaScript : When function passing in the argument it is returning ```NaN```

here is my code problem这是我的代码问题

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)-我也想知道如果我调用下面的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

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.我想当我调用 function abc(5,2,abc(5,abc(5,4,4),3))时应该返回所有参数,例如10 20 20 15用于调用 function - abc2 abc(5,2,abc(5,abc(5,4,4),3))如果我在 function 参数/参数中放置相同的 function。

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 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不是30 30 20


if I do like如果我喜欢

 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;
}

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(5,abc(abc(5,abc(5,6,6),abc(5,abc(5,6,6),4))))以与上面相同的方式返回编辑。

Your function abc(num,x,y) returns a string, which then cannot be used arithmetically in the next function.您的 function abc(num,x,y)返回一个字符串,然后不能在下一个 function 中使用算术运算。

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 返回一个字符串类型,您尝试将其与将导致 NaN 的数字相乘,在调用 abc 时尝试使用 parseInt 进行转换,应该没问题。

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"

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM