Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- Happy Monday daily_programmer!
- 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:
- 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, ...
- By considering the terms in the Fibonacci sequence whose values do not exceed 10,000, find the sum of the even-valued terms.
- Please DM me with ideas for future problems. When you have completed it post a link to your solution.
- *if you have a question about someones solution please use a thread under their posted link*
- */
- // fibonacci function
- function fibonnaci(length) {
- const result = [1, 2];
- for (let i = 0; i < (length - 2); i++) {
- const current = result[i] + result[i + 1];
- if (current >= 10000) {
- i = length; // stop the loop if value exceeds 10,000
- } else {
- result.push(current);
- }
- }
- return result;
- }
- // get fibonacci values up to 10,000
- const fibonacciArray = fibonnaci(100);
- // filter to get only the even values
- const evenFibonacciArray = fibonacciArray.filter((value) => !(value % 2));
- // add those even values up
- const result = evenFibonacciArray.reduce((prev, cur) => prev + cur);
- // log our answer
- console.log('result', result);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement