scrimba
Solution for day 9 of #javascriptmas
Go Pro!Bootcamp

Bootcamp

Study group

Collaborate with peers in your dedicated #study-group channel.

Code reviews

Submit projects for review using the /review command in your #code-reviews channel

Solution for day 9 of #javascriptmas
AboutCommentsNotes
Solution for day 9 of #javascriptmas
Expand for more info
main.js
run
preview
console
sumOddFibonacciNumbers=n=>[1,1,3,5,13,21,55,89,233,377,987].reduce((a,v)=>a<n?a+v:a,0)
////////10////////20////////30////////40////////50////////60////////70////////80////////90///////100

// [DAY 9] score: 86

/*
** javascriptmas golf rules:
** - the original test suite must pass and not be modified
** - the solution must generally work (edge cases aren't important unless stated in the problem)
** - each character in the code above counts as a stroke (including whitespace)
** - lowest score wins
*/

/**
* Test Suite
*/
describe('sumOddFibonacciNumbers()', () => {
it('returns sum of all odd Fibonnci numbers', () => {
// arrange
const num = 10;

// act
const result = sumOddFibonacciNumbers(num);

// log
console.log("result 1: ", result);

// assert
expect(result).toBe(10);
});

it('returns sum of all odd Fibonnci numbers 2nd example', () => {
// arrange
const num = 1000;

// act
const result = sumOddFibonacciNumbers(num);

// log
console.log("result 2: ", result);

// assert
expect(result).toBe(1785);
});
});


// score: 86
// works up to (n <= 1785)
// [1,1,3,5,13,21,55,89,233,377,987].reduce((a,v)=>a<n?a+v:a,0)

// score: 101
// works up to (n <= 14328)
// [1,1,3,5,13,21,55,89,233,377,987,1597,4181,6765].reduce((a,v)=>a<n?a+v:a,0)

// score: 119
// works up to (n <= 135721)
// [1,1,3,5,13,21,55,89,233,377,987,1597,4181,6765,17711,28657,75025].reduce((a,v)=>a<n?a+v:a,0)

// score: 512
// works for all javascript integers (n <= Number.MAX_SAFE_INTEGER)
// [1,1,3,5,13,21,55,89,233,377,987,1597,4181,6765,17711,28657,75025,121393,317811,514229,1346269,2178309,5702887,9227465,24157817,39088169,102334155,165580141,433494437,701408733,1836311903,2971215073,7778742049,12586269025,32951280099,53316291173,139583862445,225851433717,591286729879,956722026041,2504730781961,4052739537881,10610209857723,17167680177565,44945570212853,72723460248141,190392490709135,308061521170129,806515533049393,1304969544928657,3416454622906707,5527939700884757].reduce((a,v)=>a<n?a+v:a,0)


/*
** use the following code to pre-generate a list of
** all odd fibonacci integers javascript can handle
**

let fibs = [1,1]
let oddFibs = [1,1]
let i = 0
let t = 0

while (t < Number.MAX_SAFE_INTEGER) {
t = fibs[i] + fibs[i+1]
fibs.push(t)
if (t%2===1) oddFibs.push(t)
i++
}

console.log(oddFibs)

**
*/
Console
"result 1: "
,
10
,
"result 2: "
,
1785
,
/index.html
LIVE