DEV Community

Discussion on: Daily Challenge #153 - Horse Race Gamble

Collapse
 
centanomics profile image
Cent • Edited

Good old factorials. I had fun with this one. Feel free to give advice if I can make my code better!

JS

CodePen

Edit: forgot to convert the input to a number making all of the numbers that go through the function a string


// grabs the input, the number of horses, from the input tag
const calculate = (horses = Number(document.querySelector("#horses").value)) => {

  //puts the answer into an empty div
  document.querySelector("#answer").innerHTML = 
    horses !== parseInt(horses) ? undefined :
    horses < 4 ? horses :
    factorial(horses)/factorial(horses - 3);
}

const factorial = (n) => {
  return n ? n * factorial(n - 1) : 1;
}