
- Javascript Basics Tutorial
- Javascript - Home
- Javascript - Overview
- Javascript - Syntax
- Javascript - Enabling
- Javascript - Placement
- Javascript - Variables
- Javascript - Operators
- Javascript - If...Else
- Javascript - Switch Case
- Javascript - While Loop
- Javascript - For Loop
- Javascript - For...in
- Javascript - Loop Control
- Javascript - Functions
- Javascript - Events
- Javascript - Cookies
- Javascript - Page Redirect
- Javascript - Dialog Boxes
- Javascript - Void Keyword
- Javascript - Page Printing
- JavaScript Objects
- Javascript - Objects
- Javascript - Number
- Javascript - Boolean
- Javascript - Strings
- Javascript - Arrays
- Javascript - Date
- Javascript - Math
- Javascript - RegExp
- Javascript - HTML DOM
- JavaScript Advanced
- Javascript - Error Handling
- Javascript - Validations
- Javascript - Animation
- Javascript - Multimedia
- Javascript - Debugging
- Javascript - Image Map
- Javascript - Browsers
- JavaScript Useful Resources
- Javascript - Questions And Answers
- Javascript - Quick Guide
- Javascript - Functions
- Javascript - Resources
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 Articles
- Swift Program to Find Sum of Even Fibonacci Terms Till number N
- 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
- Determining sum of array as even or odd in JavaScript
- Sum of squares of Fibonacci numbers in C++
- Program to find Nth Even Fibonacci Number in C++
- Sum of even numbers up to using recursive function in JavaScript
- The number of terms of an A.P. is even ; sum of all terms at odd places and even places are $24$ and $30$ respectively . Last term exceeds the first term by $10.5$. Then find the number of terms in the series.
- The Fibonacci sequence in Javascript
- Fibonacci like sequence in JavaScript
- Sum of individual even and odd digits in a string number using JavaScript
- Sum of even numbers from n to m regardless if nm JavaScript
- Checking for Fibonacci numbers in JavaScript
- Nth element of the Fibonacci series JavaScript

Advertisements