簡體   English   中英

你能幫我我想打印階乘 4 的值但是下面的函數總是給 NAN 為什么?

[英]Can you help me I want to print the value of factorial 4 but the following function is always giving NAN why?

我的問題是關於遞歸函數,為什么它總是顯示 NAN 而不是顯示階乘 4 的值。

function factorial(i){
              if (i == 1) { return;}  
              return i * factorial(--i);};        
      console.log(factorial(4));
function factorial(i){
              if (i == 1) { return 1;}  
              return i * factorial(--i);};        
      console.log(factorial(4));

if (i == 1) { return;} 你應該返回 number 聽到 return 1;

function factorial(i){
              if (i == 1) { return 1;}  
              return i * factorial(--i);};        
      console.log(factorial(4));

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM