r/learnjavascript • • 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

Post image
243 Upvotes

56 comments sorted by

View all comments

11

u/Loud_Type_1756 Nov 23 '21

I am a newbie and totally unable to grasp this solution. 😔

8

u/Darkthw Nov 23 '21

I'm a newbie just like you, I've never build anything. Right now I don't have a PC so I've been doing a lot of codewars on my phone. I've learned a lot just from Googling things I can't do, never done any course just YouTube videos.

6

u/[deleted] Nov 23 '21 edited Nov 23 '21

Please look into the actual ways to do this task, you can't write these in production.

Btw great job grasping one key concept & solving it by yourself

4

u/Darkthw Nov 23 '21

Will do. What do you mean by " in production "

8

u/[deleted] Nov 23 '21 edited Nov 23 '21

By production I meant when your code will be available for public use, for example we are using reddit, we are production users of reddit, there are other internal users of reddit who are involved with the development of the app/website.

When you are creating a software to be used by people other than you ( more than 100 may be a good number) you should write tight n neat code, leaving less room for bugs. The way you converted string to integer is error prone and might cause lot of bugs.

Dont get intemidated by what I am saying, start slowly you will learn over the years, the more u write the more u learn.

Edit: crossed out the line that was wrong and may cause confusion to new guys

7

u/IronDicideth Nov 23 '21

The way you converted string to integer is error prone and might cause lot of bugs.

I would reconsider this statement. It can be misleading to someone with less experience in JS. From what I see, there is nothing in his solution that would cause a bug that would not be caused by any other solution that requires the string to be coerced into a number. Maybe you see something I do not? It is important to point out that there IS something off about the code but it is not good to convince someone that their solution is particularly buggy if it is equivalent (essentially) to any solution we would have come up with under the same circumstances. Even the cleanest solution to the code would face the same issues as OPs solution.

const stringToNumber = Number

It is a roundabout way to solve the problem but that is to be expected based on what u/Darkthw has already stated about their experience and goal with solving this problem.

1

u/[deleted] Nov 25 '21

[removed] — view removed comment

1

u/IronDicideth Nov 28 '21

The problem presented by the OP is asking you to make a function that takes a String type and turns it into a Number type. It just so happens that the Number function can already do that.

Number is both a constructor and a function. In the case of the function, any value passed to it will be coerced into a number type primitive and returned. This makes it so that assigning the Number function as seen here: const stringToNumber = Number, will simply provide another name for the Number function by which it can also be called.

On the other hand, when using the new keyword, Number is being called as a constructor and must return an instance object so that when you call it as you mentioned: const stringToNumber = new Number, all you are doing is calling the constructor with the default argument (undefined). This executes it (new keyword forces execution) and thus you now have an instance object of type Number with a primitive value of zero (undefined is coerced to zero).

This is the difference between having the constructor/function at hand to call it on something in order to turn that something into a number and having the object after the constructor has been called.

const stringToNumber = Number

Number.name // 'Number'

stringToNumber.name === Number.name // true
stringToNumber('42') === Number('42') // true

typeof stringToNumber // 'function'
typeof stringToNumber('42') // 'number'

const stringToNumber2 = new Number

stringToNumber2.name // undefined

stringToNumber2.name === Number.name // false
stringToNumber2('42') === Number('42') // Uncaught TypeError: stringToNumber2 is not a function

typeof stringToNumber // 'object'
typeof stringToNumber('42') // Uncaught TypeError: stringToNumber2 is not a function

Just as an aside, there are very few reasons why you would ever use the new keyword for any of the primitive constructors (and no good reasons IMHO). I read a few scenarios a long time ago and though I do not remember the scenarios I do remember finding it hard to see them as useful.

Let me know if this explanation answers your question.

2

u/[deleted] Nov 28 '21

[removed] — view removed comment

1

u/IronDicideth Nov 29 '21

If by "direct approach" you mean explicit then these are your best bet.

All these are constructor functions in js:

Boolean

String

Number

BigInt

Symbol

Other than these, there are operators that implicitly convert types too. The + unary operator is a good example. It essentially behaves like the Number constructor.

→ More replies (0)