Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/384.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/389.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 给定前两个整数,求斐波那契序列的第n个元素_Javascript - Fatal编程技术网

Javascript 给定前两个整数,求斐波那契序列的第n个元素

Javascript 给定前两个整数,求斐波那契序列的第n个元素,javascript,Javascript,我在做一些练习,我遇到了一个问题,我很难集中注意力 给出斐波那契序列的前两个整数。然后找到序列的第n个元素 例如,给定序列2,4,输出第4个元素。答案是10,因为: 2,4,6,10 我将如何使用JavaScript编写此解决方案(有递归和无递归?无递归: function Fibonacci(first, second, n){ let iter = 3; //set it to 3 since you are passing the first and second

我在做一些练习,我遇到了一个问题,我很难集中注意力

给出斐波那契序列的前两个整数。然后找到序列的第n个元素

例如,给定序列2,4,输出第4个元素。答案是10,因为:

2,4,6,10

我将如何使用JavaScript编写此解决方案(有递归和无递归?

无递归:

function Fibonacci(first, second, n){
      let iter = 3; //set it to 3 since you are passing the first and second

      //Use while loop instead of recursive calls to Fibonacci
      while(iter < n){
           let temp = first;
           first = second;
           second = temp + first;
           iter++;
      }

      //If n is one, return first
      if(n == 1)
         return first;

      //Display last item in sequence
      console.log(second);

       //Or return it
      return second;
}

下面是一个使用递归的解决方案:

function Fibonacci(first, second, n){
      let iter = 3; //set it to 3 since you are passing the first and second

      //Use while loop instead of recursive calls to Fibonacci
      while(iter < n){
           let temp = first;
           first = second;
           second = temp + first;
           iter++;
      }

      //If n is one, return first
      if(n == 1)
         return first;

      //Display last item in sequence
      console.log(second);

       //Or return it
      return second;
}
函数nthFib u0,u1,n{ 返回n>1?n光纤u1,u0+u1,n-1:u0; }
console.log nthFib 2,4,4;大家好,欢迎来到Stack Overflow!请注意,堆栈溢出是获取编程问题特定答案的地方,而不是代码编写服务。请自己努力解决这个问题,一旦您有了一些代码和特定的问题,请回来。考虑一下这篇有帮助的文章,请向我们展示一下你所尝试过的。递归版本不起作用。对于Fibonacci2,4,4,2,4,5,2,4,6等等,它返回6,但是对于那些,它应该返回10,16和26。两个版本都返回了错误的n=1值。在这种情况下,它们应该首先返回,而不是第二个。@Paulpro您错了。递归函数确实为Fibonacci2、4、4返回10,为2、4、5返回16。我只是测试了一下,确定一下。它会在某个时候记录下来,但不会返回。移除函数中的“console.log”,然后运行console.log Fibonacci2,4,5;你会明白我的意思。我认为只要把return放在内部递归调用之前,就可以解决这个问题。@Paulpro好的调用,我忘记了做一个return语句,这就是我用C写这篇文章并试图将其转换为javascript的结果。