DEV Community

loading...

Daily Challenge #156 - Dave's Gamble

dev.to staff
The hardworking team behind dev.to ❤️
・1 min read

My mate Dave LOVES gambling. He goes to the horse races every Saturday and bets money on which horses will finish FIRST, SECOND and THIRD place.

I'm a good friend, so I said I'd help him figure out how many different ways the gold, silver, and bronze could be handed out to the competitors so he can calculate his odds of winning.

Problem is, Dave never knows how many horses will be entering the race until he gets to the track. I guess I'll have to design a function to help me!

Write a program that can take any number of horses as its only argument and returns the total number of different combinations of competitors winning gold silver and bronze.

For example:

horses(15) -> 2730

There are effectively 2730 different combinations of 1st, 2nd and 3rd finishers.

The function should return undefined if the object entered isn't an integer.

If the number of horses is lower than 3, return the input value.

Other examples:

horses(4) -> 24
horses(1) -> 1


This challenge comes from tu6619 on CodeWars. Thank you to CodeWars, who has licensed redistribution of this challenge under the 2-Clause BSD License!

Want to propose a challenge idea for a future post? Email yo+challenge@dev.to with your suggestions!

Discussion (4)

Collapse
wheatup profile image
Hao

JavaScript

const horses = count => Number.isInteger(count) ? (
    count < 3 ? count : count * (count - 1) * (count - 2)
) : undefined;

console.log(horses(2));     // 2
console.log(horses(15));    // 2730
console.log(horses(13.37)); // undefined
Collapse
nickholmesde profile image
Nick Holmes

Isn't this the exact same challenge as from a couple of days ago?

Collapse
savagepixie profile image
SavagePixie

It is indeed, it was challenge #153