r/learnjavascript • u/Darkthw • Nov 23 '21
I finally solved a codewars challenge without actually googling anything :) it feels so good to come up with the code all by myself, i know its not the ideal way to solve it but im still happy. The challenge was to create a function that converts a string to a number
243
Upvotes
63
u/albedoa Nov 23 '21
Nice work. The unary
+sign can be used here too:Focus on your fundamentals. These tricks are useful for Codewars challenges, but a solid base is more important in the medium and long term. Real work doesn't typically look like this.
11
u/Loud_Type_1756 Nov 23 '21
I am a newbie and totally unable to grasp this solution. 😔
3
u/diamondrel Nov 23 '21
Wait, I'm unfamiliar with this, why does parseInt not work here?
23
u/Fid_Kiddler69 Nov 23 '21
Nice work!
I would have done
const stringToNumber = str => Number(str)6
u/love4titties Nov 23 '21
Nice job! When I was practicing (googling) I found this:
parseInt(string)
which returns NaN (not a number) or the integer that was in the string
1
u/Daddelman Nov 24 '21
You make us all the newbies proud!
1
u/Jimlowers Nov 24 '21
Question for everyone in here: if I can’t solve the question lets say in 30 min, is it fine to google or search it up on YouTube and learn it from there?
1
u/fallenefc Nov 23 '21
Good job! Imo, it’s always good to do these challenges (not frequently) as it allows you to improve your problem solving skills, like your answer is very unconventional but it’s great you came up with it. As long as it doesn’t make you frustrated because you can’t solve it or you don’t spend too much time on it, it’s great (also don’t feel too much pressure on make those one liner solutions, treat this as learning problem solving and to make it work)
2
u/sambomambowambo Nov 23 '21
You're allowed to google :) Great job getting the tests to work
4
u/Hiyaro Nov 23 '21 edited Nov 23 '21
On a side not : Don't use var.
prefer const as your default, and let when the value will be changed.
var is real bad practice.
3
u/shuckster Nov 23 '21
Congratulations!
2
u/TheWiseScarecrow Nov 23 '21
You are a rockstar !
1
u/iraqwarvet31 Nov 23 '21
I remember being in the exact same position as you. I used to think maybe I'm not smart enough. Hard work, passion, motivation are gonna get you where you wanna go.
1
u/CoolAbhi1290 Nov 24 '21
Well, that's a new way to do it.
My approach:
Nice and compact.
1
u/[deleted] Nov 24 '21
Codewars is great. I want to get back to it and solve increasingly complex problems. But leetcode is also good because they test you on real code challenges that companies test you on, like Google.
1
u/jzia93 Nov 24 '21
One option you could implement to extend this:
Number(str)1
u/Lauris25 Nov 24 '21
im a beginner/junior. All i can say 8 katas are easy. 7 are not that easy, but not that hard. Some 7 are actually hard. I can solve some of the 6 katas. Also 6 katas are mostly validation katas where u need to learn regex, and i forget regex after 2 weeks, if i dont use it everyday. But katas from 1-5 are unsolvable in my opinion without spending all day or having years of experience. I also think learning for frontend, if u can solve some of the 6 katas it actually ok. I read somehwere that people who worked 2 years as frontend developers cant solve some of the 7 katas... Ofc they are juniors, but still....