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 #26
DESCRIPTION:
Your classmates asked you to copy some paperwork for them. You know that there are 'n' classmates and the paperwork has 'm' pages.
Your task is to calculate how many blank pages do you need. If n < 0 or m < 0 return 0.
Example:
n= 5, m=5: 25
n=-5, m=5: 0
Starting code:
function paperwork(n, m) {
}
My attempt:
function paperwork(n, m) {
if(n < 0 || m < 0){
return 0
} else {
return n*m
}
}
Rank #1 in 'Best Practice':
function paperwork(n, m) {
return n > 0 && m > 0 ? n * m : 0
}
Rank #2:
function paperwork(n, m) {
if (m < 0 || n < 0) {
return 0;
}
return m * n;
}
Rank #3:
function paperwork(n, m) {
return n < 0 || m < 0 ? 0 : n * m;
}
Rank #4:
paperwork = (n, m) => n < 0 || m < 0 ? 0 : n * m
Weird that this one worked. I was trying to do a one-liner (almost exactly like this but with the const before function name) but the code formatting got weird when I was trying to add the second parameter for the function so I didn't risk it and went with the conventional way.
Rank #5 with the use of Math.max():
const paperwork = (n, m) => Math.max(0, n) * Math.max(0, m);
That's it for Kata #26, 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