我今天在这里,因为我有一个错误我(和我的老师)在javascript中不理解。
以下代码只是函数的一个因子(强制用函数执行)。 但是返回时我的值在undefined中被改变了。
var test; do { var result = parseInt(prompt("Un chiffre supérieur à 1")); } while (isNaN(result) || result < 2); // Just asking for a number > à 1 it work // test = facto(result, 1); console.log(test); // The test variable was used to try to understand the bug // function facto(chiffre, fact) { // Function for the factorial // fact = fact * chiffre; // fact was defined as 1 for the first entry, for others it's gonna be his last result //` console.log(fact); chiffre = chiffre - 1; // We decrase chiffre by one for the next passage // if (chiffre == 1) { // if chiffre = 1, we don't need to do other passage, so we return value console.log(fact); return fact; // the result is correct, it's a number. However it return undefined // } else { // if chiffre > 1, we continue the math by coming again in the function // console.log(chiffre); facto(chiffre, fact); } }我不明白为什么它返回undefined。 有人可以解释一下吗? 此外,如果函数只执行一次,该函数也可以工作。
先谢谢你。
I'm here today because i've an error I (and my teacher) don't understand in javascript.
The following code is just an factorial by a function (forced to do it with function). But my value is changed in undefined when return.
var test; do { var result = parseInt(prompt("Un chiffre supérieur à 1")); } while (isNaN(result) || result < 2); // Just asking for a number > à 1 it work // test = facto(result, 1); console.log(test); // The test variable was used to try to understand the bug // function facto(chiffre, fact) { // Function for the factorial // fact = fact * chiffre; // fact was defined as 1 for the first entry, for others it's gonna be his last result //` console.log(fact); chiffre = chiffre - 1; // We decrase chiffre by one for the next passage // if (chiffre == 1) { // if chiffre = 1, we don't need to do other passage, so we return value console.log(fact); return fact; // the result is correct, it's a number. However it return undefined // } else { // if chiffre > 1, we continue the math by coming again in the function // console.log(chiffre); facto(chiffre, fact); } }I don't understand why it return undefined. Could someone explain me ? Also, the function work if it go only one time in the function.
Thank you in advance.
最满意答案
你必须返回递归调用,否则第一次调用facto将永远不会遇到return语句,导致第一次调用(默认情况下)返回undefined 。 改为return facto(chiffre, fact); :
var test; do { var result = parseInt(prompt("Un chiffre supérieur à 1")); } while (isNaN(result) || result < 2); test = facto(result, 1); console.log(test); function facto(chiffre, fact) { fact = fact * chiffre; chiffre = chiffre - 1; if (chiffre == 1) { return fact; } else { return facto(chiffre, fact); } }You have to return the recursive call, else the first call of facto will never encounter a return statement, resulting in that first call (by default) returning undefined. Change to return facto(chiffre, fact);:
var test; do { var result = parseInt(prompt("Un chiffre supérieur à 1")); } while (isNaN(result) || result < 2); test = facto(result, 1); console.log(test); function facto(chiffre, fact) { fact = fact * chiffre; chiffre = chiffre - 1; if (chiffre == 1) { return fact; } else { return facto(chiffre, fact); } }
更多推荐
发布评论