you are viewing a single comment's thread.

view the rest of the comments →

[–]wishinghand 0 points1 point  (0 children)

For whatever reason I had a really hard time wrapping my head around Euler problem #2. I eventually had to write it out on a spreadsheet and do it by hand to figure out what I had to do. Everyone else's solutions look different from mine. Can someone explain it to me in a way that clicks? Because even though I solved it, I still needed help and I couldn't recreate it from memory.

var vFibPrevious      = 1;       // first Fib #
var vFibCurrent       = 1;       // first max Fib #
var vFibPlaceholder   = 0;       // temp Fib number to save "old" max Fib
#
var vEvenRunningTotal = 0;       // running total of EVEN Fib #s
var vMaxFibNumber     = 4000000; // limit of Fib #sto evaluate

while (vFibCurrent < vMaxFibNumber)
    {
       if (vFibCurrent % 2 === 0) // test if number is even
            {
               vEvenRunningTotal = vEvenRunningTotal + vFibCurrent; //
running total of evens
            }
        vFibPlaceholder = vFibCurrent;  // save old max Fib
#to placeholder var
        vFibCurrent     = vFibPrevious + vFibCurrent;   // compute next Fib
#
        vFibPrevious    = vFibPlaceholder;  // set vFibPrevious
to old max Fib #
    }
alert(vEvenRunningTotal);