greatestProduct3

by not important

HTML

<script src="//searls.github.io/jasmine-all/jasmine-all-min.js"></script>

CoffeeScript

# greatest product of 3
greatestProducts3 = (inputInts) ->
    throw 'input must have at least 3 numbers' if inputInts.length < 3
    highestProductOf3 = -Infinity
    highestProductOf2 = -Infinity
    highest = -Infinity
    lowestProductOf2 = Infinity
    lowest = Infinity
    for int, index in inputInts
        if index > 1
            highestProductOf3 = int * highestProductOf2 if int * highestProductOf2 > highestProductOf3
            highestProductOf3 = int * lowestProductOf2 if int * lowestProductOf2 > highestProductOf3
        if index > 0
            highestProductOf2 = int * highest if int * highest > highestProductOf2
            highestProductOf2 = int * lowest if int * lowest > highestProductOf2
            lowestProductOf2 = int * highest if int * highest < lowestProductOf2
            lowestProductOf2 = int * lowest if int * lowest < lowestProductOf2
        highest = int if int > highest
        lowest = int if int < lowest
    highestProductOf3

describe 'greatestProduct3', ->
    it 'works', ->
        expect(true).toEqual true
        expect(greatestProducts3([1, 10, -5, 1, -100])).toEqual 5000