

- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Sum of even Fibonacci terms in JavaScript
We are required to write a JavaScript function that takes in a number as a limit. The function should calculate and return the sum of all the Fibonacci numbers that are both smaller than the limit and are even.
For example −
If the limit is 100
Then the even Fibonacci terms are −
2, 8, 34
And the output should be −
44
Example
Following is the code −
const sumOfEven = (limit) => { let temp, sum = 0, a = 0, b = 1; while (b < limit) { if (b % 2 === 0) { sum += b; }; temp = a; a = b; b += temp; }; return sum; }; console.log(sumOfEven(100)); console.log(sumOfEven(10)); console.log(sumOfEven(1000));
Output
Following is the output on console −
44 10 798
- Related Questions & Answers
- Minimum Fibonacci terms with sum equal to K in C++
- Java Program to Find Even Sum of Fibonacci Series till number N
- Even index sum in JavaScript
- Sum of squares of Fibonacci numbers in C++
- Determining sum of array as even or odd in JavaScript
- Sum of even numbers up to using recursive function in JavaScript
- Program to find Nth Even Fibonacci Number in C++
- Sum of Even Numbers After Queries in Python
- The Fibonacci sequence in Javascript
- Fibonacci like sequence in JavaScript
- Sum of individual even and odd digits in a string number using JavaScript
- Nth element of the Fibonacci series JavaScript
- Find sum of the series ?3 + ?12 +.... upto N terms in C++
- Even Number With Prime Sum
- Find sum of even index binomial coefficients in C++
Advertisements