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 #37
DESCRIPTION:
Note: This kata is inspired by Convert a Number to a String!. Try that one too.
Description
We need a function that can transform a string into a number. What ways of achieving this do you know?
Note: Don't worry, all inputs will be strings, and every string is a perfectly valid representation of an integral number.
Examples
"1234" --> 1234
"605" --> 605
"1405" --> 1405
"-7" --> -7
Starting code:
const stringToNumber = function(str){
// put your code here
return null;
}
My attempt:
const stringToNumber = str => Number(str)
Rank #1 in 'Best Practice':
var stringToNumber = function(str){
return parseInt(str);
}
Rank #2:
var stringToNumber = function(str){
// put your code here
return Number(str);
}
Rank #3:
var stringToNumber = function(str){
return +str;
}
Rank #4:
const stringToNumber = str => Number(str)
Rank #5:
var stringToNumber = Number;
And in rank #6 we can see a troll that goes by a.vadim:
const ways = [
s => +s,
s => s++, // why does it work?
s => s--,
s => ++s - 1,
s => --s + 1,
s => s * 1,
s => s - 0,
s => s / 1,
s => s - '',
s => s ** 1,
s => s % Infinity,
s => ~~s,
s => s & -1,
s => s | 0,
s => s ^ 0,
s => s << 0,
s => s >> 0,
s => s | s,
s => s & s,
s => ~s ^ -1,
s => ~s ^ ~0, // ~0 = -1
s => ~(s ^ -1),
s => s * !!s,
s => -1 * (~s + 1) || 0, // -1 * 0 = -0
s => -(~(s >>> 0) + 1) || 0,
Number,
parseInt,
parseFloat,
s => Number(s),
s => parseInt(s),
s => parseFloat(s),
s => Math.floor(s),
s => Math.round(s),
s => Math.ceil(s),
s => Math.min(s),
s => Math.max(s),
Math.floor,
Math.round,
Math.ceil,
Math.min,
Math.max,
s => eval(`+ ${s}`), // a space is needed
s => eval('+ ' + s), // for cases like eval(+ +12345)
s => [...s]
.slice(+(s[0] == '-' || s[0] == '+')) // remove the sign, if present
.reduce((acc,x) => +x + acc * 10)
* (s[0] == '-' ? -1 : 1),
s => [...s]
.slice(+(s[0] == '+')) //remove +, if present
.reverse()
.reduce((acc,x,i) => x == '-' ? -acc : acc += x * 10 ** i, 0),
s => ways[~~(Math.random() * ways.length)](s)
];
// And now we will use all the methods in a row
const checkThemAll /* © Mylene Farmer */ = (acc, x) => acc === x ? x : NaN;
const stringToNumber = str => ways.map(fn => fn(str)).reduce(checkThemAll);
That's it for Kata #37, 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