If you missed the introductory post, it's here. For a list of previously solved katas, please refer to the bottom of this page.
If this is your first time seeing my post, please note - these katas are probably randomly assigned per user so please don't go into Codewars thinking we will have exactly the same user experience, I am just posting these in the order that I got them.
Kata #40
DESCRIPTION:
In mathematics, the factorial of integer 'n' is written as 'n!'. It is equal to the product of n and every integer preceding it. For example: 5! = 1 x 2 x 3 x 4 x 5 = 120
Your mission is simple: write a function that takes an integer 'n' and returns 'n!'.
You are guaranteed an integer argument. For any values outside the positive range, return null, nil or None .
Note: 0! is always equal to 1. Negative values should return null;
For more on Factorials : http://en.wikipedia.org/wiki/Factorial
Starting code:
//This function should return n!
function factorial (n) {
}
My attempt that worked:
//This function should return n!
function factorial (n) {
let result = 1
if (n > 0) {
for (i = n ; i > 1 ; i--){
result *= i
}
return result
} else if (n == 0){return 1}
else {return null}
}
Rank #1 in 'Best Practices':
//This function should return n!
function factorial (n) {
if (n > -1)
return n == 0 ? 1 : n * factorial(n - 1);
}
Wow. Gotta admit I got got. Also have to admit I had to look up if there was a built-in factorial function (there wasn't) and realized this was my first taste of recursion challenge in the wild. Although the code I cooked up worked, I'm jealous of those that looks so much cleaner.
Rank #2 also a recursion:
//This function should return n!
function factorial (n) {
if (n < 0) return null;
if (n === 0) return 1;
return n * factorial(--n)
}
Rank #3 exactly the same as rank #2 but somehow got detected as different, with the difference only being spaces and semicolons:
//This function should return n!
function factorial (n) {
if (n < 0) return null
if (n === 0) return 1
return n * factorial(n-1)
}
Rank #4 with yet another recursion :
//This function should return n!
function factorial(n) {
return n>=0 ? n ? n*factorial(n-1) : 1 : null;
}
I mistakenly thought I couldn't use .reduce() because the argument passed to the function is not an array but rank #5 by user andorey demonstrates it here:
// n!
function factorial (n) {
return n < 0 ? null : Array.from({length: n}, (_,i)=> i+1).reduce((a,b)=> a*b,1)
}
Rank #6 with a for loop by user g964:
function factorial(n) {
if (n < 0) return null;
var val = 1;
for (var i = 2; i <= n; i++)
val = val * i;
return val;
}
Then ranks #7 to #10 also used the recursive solution. Which should probably beg the question, why didn't I think of that? Well because recursion hasn't come up yet in my study material, that's why ¯\_(ツ)_/¯ 🤣
That's it for Kata #40, stay tuned for more katas to be solved!
Link to Kata #1: Square(n) Sum
Link to Kata #2: Convert a Number to a String
Link to Kata #3: DNA to RNA Conversion
Link to Kata #4: Remove First and Last Character
Link to Kata #5: MakeUpperCase
Link to Kata #6: Total amount of points
Link to Kata #7: A Needle in the Haystack
Link to Kata #8: Sum of positive
Link to Kata #9: Basic Mathematical Operations
Link to Kata #10: Beginner - Reduce but Grow
Link to Kata #11: Square Every Digit
Link to Kata #12: Friend or Foe?
Link to Kata #13: Grasshopper - Summation
Link to Kata #14: Get the Middle Character
Link to Kata #15: Descending Order
Link to Kata #16: String ends with?
Link to Kata #17: Sum of two lowest positive integers
Link to Kata #18: Sum of odd numbers
Link to Kata #19: Find the next perfect square!
Link to Kata #20: Reversed Strings
Link to Kata #21: Jaden Casing Strings
Link to Kata #23: Returning Strings
Link to Kata #24: Opposite number
Link to Kata #25: Are You Playing Banjo?
Link to Kata #26: Beginner Series #1 School Paperwork
Link to Kata #27: Remove String Spaces
Link to Kata #28: Invert values
Link to Kata #30: Beginner Series #2 Clock
Link to Kata #31: Is this a triangle?
Link to Kata #32: Sum of the first nth term of Series
Link to Kata #33: Find the smallest integer in the array
Link to Kata #34: Simple multiplication
Link to Kata #35: How good are you really?
Link to Kata #36: Who likes it?
Link to Kata #37: Convert a String to a Number!