Oliver Eyton-Williams bd68b70f3d
Feat: hide blocks not challenges (#39504)
* fix: remove isHidden flag from frontmatter

* fix: add isUpcomingChange

Co-authored-by: Ahmad Abdolsaheb <ahmad.abdolsaheb@gmail.com>

* feat: hide blocks not challenges

Co-authored-by: Ahmad Abdolsaheb <ahmad.abdolsaheb@gmail.com>

Co-authored-by: Ahmad Abdolsaheb <ahmad.abdolsaheb@gmail.com>
2020-09-03 15:07:40 -07:00

1.8 KiB

id, title, isRequired, challengeType, forumTopicId
id title isRequired challengeType forumTopicId
a5229172f011153519423690 Sum All Odd Fibonacci Numbers true 5 16084

Description

Given a positive integer num, return the sum of all odd Fibonacci numbers that are less than or equal to num. The first two numbers in the Fibonacci sequence are 1 and 1. Every additional number in the sequence is the sum of the two previous numbers. The first six numbers of the Fibonacci sequence are 1, 1, 2, 3, 5 and 8. For example, sumFibs(10) should return 10 because all odd Fibonacci numbers less than or equal to 10 are 1, 1, 3, and 5.

Instructions

Tests

tests:
  - text: <code>sumFibs(1)</code> should return a number.
    testString: assert(typeof sumFibs(1) === "number");
  - text: <code>sumFibs(1000)</code> should return 1785.
    testString: assert(sumFibs(1000) === 1785);
  - text: <code>sumFibs(4000000)</code> should return 4613732.
    testString: assert(sumFibs(4000000) === 4613732);
  - text: <code>sumFibs(4)</code> should return 5.
    testString: assert(sumFibs(4) === 5);
  - text: <code>sumFibs(75024)</code> should return 60696.
    testString: assert(sumFibs(75024) === 60696);
  - text: <code>sumFibs(75025)</code> should return 135721.
    testString: assert(sumFibs(75025) === 135721);

Challenge Seed

function sumFibs(num) {
  return num;
}

sumFibs(4);

Solution

function sumFibs(num) {
  var a = 1;
  var b = 1;
  var s = 0;
  while (a <= num) {
    if (a % 2 !== 0) {
      s += a;
    }
    a = [b, b=b+a][0];
  }
  return s;
}