Advertisement
Guest User

Untitled

a guest
Mar 27th, 2017
46
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.21 KB | None | 0 0
  1. /*
  2. Happy Monday daily_programmer!
  3.  
  4. Each new term in the Fibonacci sequence is generated by adding the previous two terms. By starting with 1 and 2, the first 10 terms will be:
  5.  
  6. 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, ...
  7.  
  8. By considering the terms in the Fibonacci sequence whose values do not exceed 10,000, find the sum of the even-valued terms.
  9.  
  10. Please DM me with ideas for future problems. When you have completed it post a link to your solution.
  11.  
  12. *if you have a question about someones solution please use a thread under their posted link*
  13. */
  14.  
  15. // fibonacci function
  16. function fibonnaci(length) {
  17. const result = [1, 2];
  18. for (let i = 0; i < (length - 2); i++) {
  19. const current = result[i] + result[i + 1];
  20. if (current >= 10000) {
  21. i = length; // stop the loop if value exceeds 10,000
  22. } else {
  23. result.push(current);
  24. }
  25. }
  26. return result;
  27. }
  28.  
  29. // get fibonacci values up to 10,000
  30. const fibonacciArray = fibonnaci(100);
  31.  
  32. // filter to get only the even values
  33. const evenFibonacciArray = fibonacciArray.filter((value) => !(value % 2));
  34.  
  35. // add those even values up
  36. const result = evenFibonacciArray.reduce((prev, cur) => prev + cur);
  37.  
  38. // log our answer
  39. console.log('result', result);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement