【问题标题】:How to check if number is even using recursive function in JavaScript如何检查数字是否甚至在JavaScript中使用递归函数
【发布时间】:2018-12-21 10:03:10
【问题描述】:

我正在尝试从这里做一个练习https://www.w3resource.com/javascript-exercises/javascript-recursion-function-exercise-7.php

我了解解决方案,但是,我有点好奇为什么我的程序没有产生与上述相同的答案。我选择返回函数的方式略有不同,但是,它产生一个数字而不是真或假。

function checkeven(num) {
    if (num === 0) {
        return true;
    } else if (num === 1) {
        return false;
    } else {
        console.log(num);
        return num - checkEven(num - 2);
    }
}

   
console.log(checkeven(8));
console.log(checkeven(9));

【问题讨论】:

  • 你终于减去了一个布尔值。使用减法后,您期望哪个值?
  • return num - checkeven(num - 2) -> return checkeven(num - 2)
  • return num - checkeven(num - 2); 应该是return checkeven(num - 2);
  • Improve this sample solution 不要使用递归,:)
  • 我见过的最奇怪的递归用例

标签: javascript function recursion


【解决方案1】:

在 JavaScript 中 - 当运算符的操作数属于不同类型时 - 会发生类型转换,即它会尝试将其中一个操作数转换为与运算符兼容的类型

试试这个: console.log(2 + true) //3

console.log(true + false) //1

在你的情况下,考虑一下当你尝试checkeven(2)时会发生什么

checkeven(2) = 2 - checkeven(0) = 2 - true = 1

【讨论】:

  • 谢谢@Cally。如果我的回答解决了您的问题,请接受并关闭问题
【解决方案2】:

在您的代码中将return num - checkeven(num - 2) 替换为return checkeven(num - 2)

function checkeven(num) {
    if (num === 0) {
        return true;
    } else if (num === 1) {
        return false;
    } else {
        return checkeven(num - 2);
    }   
} 
console.log(checkeven(8));
console.log(checkeven(9));

【讨论】:

    【解决方案3】:

    用正数和负数设计了一个解决方案!

      var isEven = function (n) {
      if (n > 0) {
        return positive(n);
      } else {
        return negative(n);
      }
    
      function negative(num) {
        if (num === 0) {
          return true;
        } else if (num === -1) {
          return false;
        } else {
          return isEven(num + 2);
        }
      }
    
      function positive(num) {
        if (num === 0) {
          return true;
        } else if (num === 1) {
          return false;
        } else {
          return isEven(num - 2);
        }
      }
    };
    
    console.log(isEven(9));
    console.log(isEven(8));
    console.log(isEven(-8));

    【讨论】:

      【解决方案4】:

      您的代码有问题,因为您我们试图从布尔值中减去,由于根据 ecmascript guidlines 下给出的规则发生了哪种类型的 corecion。(https://www.ecma-international.org/ecma-262/7.0/#sec-ordinary-and-exotic-objects-behaviours)

      您可以使用以下解决方案:

        (function fixthis() {
            function checkeven(num) {
      
                if (num === 0) {
                    return true;
                } else if (num === 1) {
                    return false;
                } else {
      
                    let Tempnum = num - 2;
                    if (checkeven(Tempnum)) {
                        return true;
      
                    } else {
                        return false;
                    }
                }
            }
            console.log(checkeven(8));
            console.log(checkeven(9));
      
        })()
      

      【讨论】:

        【解决方案5】:

        return checkeven(num-2);,返回一个布尔值。您试图从一个数字中减去一个布尔值,这是行不通的。如果您删除 num-checkeven(num-2)* 并将其替换为 checkeven(num-2);,您的代码将正常工作

        【讨论】:

          【解决方案6】:

          你可以这样做:

          odd = function(n) {if (n%2 == 0) {return true} else {return false;}}
          
          console.log(odd(8));
          console.log(odd(5));

          或:

          checkeven = function(num) {
              if (num === 0) {
                  return true;
              } else if (num === 1) {
                  return false;
              } else {
                  return checkeven(num - 2);
              }
          }
                       
          console.log(checkeven(8));
          console.log(checkeven(9));

          【讨论】:

          • 您可以只使用let odd = (num%2 == 0)return (n%2 == 0) 而不使用条件运算符。
          • :) 你是对的。所以我想你可以看看它是如何工作的。
          • 结果正确,但没有回答问题
          • 好的,你是对的,但部分答案已经在我发布的内容中。函数除了数字没有返回值,return 必须再次调用回调函数……所以大致如下: checkeven = function (num) { if (num === 0) { return true; } else if (num === 1) { return false; } 其他 { n = 数字 -2;返回检查偶数(n); } } console.log(检查偶数(8)); console.log (check even (9));
          • 这种情况下n变成了全局变量,直接放在return `num - 2`里面就可以了
          【解决方案7】:

          你也可以使用模运算符,所以你甚至不需要递归

          const areYouEven = n => !(n % 2)
          

          【讨论】:

          • 可以对主要问题发表评论;未能解决实际问题或遵守链接练习的要求。
          猜你喜欢
          • 2020-11-14
          • 1970-01-01
          • 2020-05-04
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2013-11-30
          • 2015-03-19
          • 1970-01-01
          相关资源
          最近更新 更多