Skip to content
Advertisement

Using primitive operators to find factorial of N up to K depth

Having difficulty coming up with a solution using:

  1. iteration/control-flow and
  2. accumulation.

More than just a solution, would prefer having an answer with hints and explanation.

JavaScript

Advertisement

Answer

JavaScript

Ignoring k=0 you have want to multiply k numbers starting with n and ending with n-k. The above loops k times and since i will increment by 1 starting from 0, you can simply subtract it from n to get the next number to multiply by.

Edit: Making sure k=0 is returning 1 always by returning early

Edit2: Removing built in range function

Edit3: making sure to go k deep

User contributions licensed under: CC BY-SA
4 People found this is helpful
Advertisement