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 #24
DESCRIPTION:
Very simple, given an integer or a floating-point number, find its opposite.
Examples:
1: -1
14: -14
-34: 34
Starting code:
function opposite(number) {
//your code here
}
My attempt:
const opposite = number => number *= -1
Rank #1 in 'Best Practice':
function opposite(number) {
return(-number);
}
Troll town in rank #2 with 1234 'Clever' votes by users ManuSensei and xdspacex:
function opposite(number) {
/* Now this is the story all about how
My life got flipped, turned upside down
And I'd like to take a minute just sit right there
I'll tell you how I became the prince of a town called Bel-air
In west Philadelphia born and raised
On the playground where I spent most of my days
Chilling out, maxing, relaxing all cool
And all shooting some b-ball outside of the school
When a couple of guys, they were up to no good
Started making trouble in my neighbourhood
I got in one little fight and my mom got scared
And said "You're moving with your auntie and uncle in Bel-air"
I whistled for a cab and when it came near the
License plate said "fresh" and had a dice in the mirror
If anything I could say that this cab was rare
But I thought nah, forget it, yo homes to Bel-air!
I pulled up to a house about seven or eight
And I yelled to the cabby "Yo, homes smell you later!"
Looked at my kingdom I was finally there
To sit on my throne as the prince of Bel-air */
return -number;
}
Ranks #3, #4 and #5:
//Rank #3
const opposite = number => -number;
//Rank #4
function opposite(number) {
return number * (-1);
}
//Rank #5
const opposite = n => -n;
The not-sure-if-trolling or just (maybe unnecessarily) meticulous approach at rank #6 by five users gbenavid, akremdakhli, Mehdi Cherif , user9429412, rabienajlaoui :
function opposite(number) {
if (number === 0) {
return 0;
} else if (number.toString().includes('.') && number > 0) {
return parseFloat("-" + number, 10);
} else if (number > 0) {
return parseInt("-" + number, 10);
} else if (number < 0 && number.toString().includes('.')) {
var didget = number.toString().split('-').pop();
return parseFloat(didget);
} else {
var didgets = number.toString().split('-').pop();
return parseInt(didgets);
}
}
That's it for Kata #24, 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