Javascript函数transfomr我的未定义值。(Javascript function transfomr my value in undefined. Me and my teacher

编程入门 行业动态 更新时间:2024-10-26 06:36:00
Javascript函数transfomr我的未定义值。(Javascript function transfomr my value in undefined. Me and my teacher does'nt understand why)

我今天在这里,因为我有一个错误我(和我的老师)在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);
  }
} 
  
 

更多推荐

本文发布于:2023-07-29 20:44:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1319596.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:函数   未定义   transfomr   Javascript   function

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!