簡體   English   中英

為什么我的代碼使用underscore.js但不能使用Ramda.js?

[英]Why does my code work with underscore.js but not when I use Ramda.js?

我是Javascript的新手,我正在進行編碼挑戰以了解有關該語言的更多信息。 這與學校無關或類似,完全是為了我自己的個人成長。 這是挑戰:

如果它是斐波納契數,則返回所有奇數斐波那契數的總和,直至並包括傳遞的數。

我在過去的兩個晚上都在努力解決這一挑戰。 當我使用underscore.js運行我的代碼時,它可以工作。 當我使用Ramda.js時,它說NaN 我認為兩者都會回歸NaN 我很驚訝我可以從一個而不是另一個得到正確答案。 任何見解將不勝感激!

var R = require('ramda');

function sumFibs(num) {
var fib_Arr = [];
var new_Arr = [];
var total = 0;
// I use this to tell if the fib num is greater than 2
var the_Bit = "false";
// This is used to keep track of when to stop the loop
var fib_Num = 0;

// THIS WORKS FROM HERE
// This loop generates a list of fibonacci numbers then pushes them to the    fib_Arr
for(var i = 0; total < num; i++){
if (i < 1){
  fib_Arr.push(0);

}
else if (i === 1){
  fib_Arr.push(i);
  fib_Arr.push(1);
}
else if (i === 2){
  fib_Arr.push(2);
  the_Bit = "true";
}
else if (the_Bit === "true"){
  temp_Arr = R.last(fib_Arr,2);
  temp_Arr = temp_Arr[0] + temp_Arr[1];
  fib_Arr.push(temp_Arr);
  total = R.last(fib_Arr);
}
// Generating the fib Array works TO HERE!!!!
}

// console.log(fib_Arr); // Print out the generated fibonacci array
// if last Array element is greater than the original in
 var last_Element = R.last(fib_Arr);
if (last_Element > num){
  console.log("The last element of the array is bigger!");
  fib_Arr.splice(-1,1); // This removes the last item from the array if it is  larger than the original num input
 }

// This loop removes all of the EVEN fibonacci numbers and leaves all of the ODD numbers
  for (var j = 0; j < fib_Arr.length; j++){
    if (fib_Arr[j] % 2 !== 0){
      new_Arr.push((fib_Arr[j]));
    }
  }

// This checks if the original input num was a
    if (num % 2 !== 0){
    new_Arr.push(num);
    }
    else{
      console.log("The original num was not a Fibonacci number!");
    }
  // if last Array element is the same as the original input num
    var last = R.last(fib_Arr);
  if (last === num){
   console.log("Removing the last element of the array!");
    new_Arr.splice(-1,1); // This removes the last item from the array if it is the same as the original num input
  }

// Now to add all of the numbers up :-)
  for (var k = 0; k < new_Arr.length; k++){
    console.log("This is fib_Num: " + fib_Num);
    // console.log(fib_N`);
    fib_Num = fib_Num += new_Arr[k];
  }
  return fib_Num;
}
// TEST CASES:
// console.log(sumFibs(75025)); //.to.equal(135721);
console.log(sumFibs(75024)); //.to.equal(60696);

你在這些方面有問題:

temp_Arr = R.last(fib_Arr,2);
temp_Arr = temp_Arr[0] + temp_Arr[1];

除了R.last沒有采用第二個參數(盡管不會失敗)這一事實,你使用temp_arr作為數組,當它是一個數字時。 因此, temp_arr獲取NaN值。

您可能正在尋找R.take (與R.reverse合並)或R.slice


通過更改:

temp_Arr = R.last(fib_Arr,2);

用:

temp_Arr = R.take(2, R.reverse(fib_Arr));

或者:

temp_Arr = R.slice(fib_Arr.length - 2, fib_Arr.length)(fib_Arr);

或者(從右邊減少獎勵游戲):

temp_Arr = R.reduceRight(function(arr, elem) { 
    return arr.length < 2 ? [elem].concat(arr) : arr;
}, [])(fib_Arr);

我們得到:

sumFibs(75024) === 60696

為了記錄,這是你如何解決這個問題:

function fibSumTo(n) {
  var f1 = 1, f2 = 1, sum = 1, t;
  while (f2 <= n) {
    if (f2 & 1) sum += f2;
    t = f1 + f2;
    f1 = f2;
    f2 = t;
  }
  return sum;
}

實際上不需要任何類型的庫,因為實際上不需要任何類型的數據結構。

var _ = require('underscore');function sumUpFibs (number){
arr_of_fibs = [1,1];
current = 1; //cursor for previous location
while (true){
    var num = arr_of_fibs[current] + arr_of_fibs[current - 1];
    if (num <= number) {
        arr_of_fibs.push(num);
        current++;
    } else {
        break;
    }
}
console.log(arr_of_fibs);
var total = 0;
_.each(arr_of_fibs, function(fib){
    total += fib;
})
return total;}console.log(sumUpFibs(75025));

這可能是一個更好的實現...雖然我知道你剛剛開始所以我不想下定義:D ....此外,也許,檢查你的測試用例。

暫無
暫無

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

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