# 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
Please Whitelist JSFiddle in your content blocker.
Help keep JSFiddle free for always by one of two ways:
Whitelist JSFiddle in your content blocker (two clicks)
Go PRO and get access to additional PRO features →
Join the 4+ million users, and keep the JSFiddle dream alive.
Ad-free
All ads in the editor and listing pages are turned completely off.
Use pre-released features
You get to try and use features (like the Palette Color Generator) months before everyone else.
Fiddle collections
Sort and categorize your Fiddles into multiple collections.
Private collections and fiddles
You can make as many Private Fiddles, and Private Collections as you wish!
Console
Debug your Fiddle with a minimal built-in JavaScript console.