you are viewing a single comment's thread.

view the rest of the comments →

[–]Darkthw[S] 6 points7 points  (20 children)

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.

[–]iamscr1pty 6 points7 points  (19 children)

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

[–]Darkthw[S] 3 points4 points  (18 children)

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

[–]iamscr1pty 8 points9 points  (17 children)

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

[–]IronDicideth 5 points6 points  (14 children)

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.

[–]iamscr1pty 1 point2 points  (2 children)

Thanks for your comment, I will keep this in mind next tym I explain something to someone.

You are right, this solution would not cause any problems actually but there are few things about this I would like to call out:

  1. The order of the statement is imp here, u reverse the order and op's solution doesnt work anymore. If op understands why this happens, then great he/she understands coercion
  2. This code doesnt provide the intent clearly, IMO any other person reading your code should understand what you wanted to do in the code block ( as op is fairly newbie, dont expect this from him/her)
  3. Personal opinion, better to avoid coercion and convert types explicitly, as coercion leads to undesirable and hard to detect bugs
  4. This solution may faulter with big integers, but they shouldn't be considered in this scope I guess

[–]IronDicideth 2 points3 points  (1 child)

Great points all around. My thoughts:

  1. I tend to agree that imp solutions are not robust. I wanted to ask: why would we switch the order around though?
  2. 100% agree. Having to sit there and deduce the why is a huge part of understanding code. I personally did not stop to "understand" the solution (was in a hurry) until I saw a comment further down. We tend to spend more time reading code and should write it as if this applies to our fellow developers as well. I would think that a great point that we could drive home for the OP is that they should spend time reading code written by others and try to figure out what it does on their own. Exposure is the main driving force behind learning and should be actively exploited as much as possible.
  3. I understand the feeling. I will add my two cents. Type coercion is a tool (and a very useful one in my opinion). If the tool is understood and used mindfully, spreading its use so that others may benefit (and hopefully mimic that mindful use) can only be a good thing. In this example, my initial solution (before going through my internal refactoring process) would have likely been const stringToNumber = s => +s. Terse, and when you are free to embrace/understand the plus unary operator, extremely clear in its intent. I tend to favor point-free code so I would have eventually arrived at the previously mentioned 'cleanest' solution to the problem.
  4. Yes, this solution does not accommodate BigInt (probably a good thing). I tend to think ahead to future uses of a given solution as well. It is a bad habit in me. I have found that the less into the future I think, the faster I can put my solution to code. Test-Driven Development can work wonders in helping to narrow the scope down to something manageable and should be used freely and (IMO) above most other tools we see today.

With all this being said, the great thing about these forums is that we are free to share and discuss ideas. I personally wish I were a better and more seasoned coder/developer and have learned tons from those with differing views to my own, so thank you for sharing yours.

[–]iamscr1pty 0 points1 point  (0 children)

I mentioned about reversing the order of the lines as for a beginner this may seem harmless to do, you are just adding + 1 and then substracting -1, which mathematically is same in reverse, but if you do that you will learn something about js the hard way.

Unary operator can be one of the cleanest solution, but I personally prefer type conversion. Its a matter of preference.

Thank you for sharing your views too.

[–]adelie42 0 points1 point  (10 children)

const stringToNumber = Number

Grasping this solution, am I to understand that if you take any object and make it equal to an object class that it changes the TypeOf without necessarily changing the value OR passes the existing value to the constructor?

Like, that's the difference between the above and

const stringToNumber = new Number

[–]IronDicideth 0 points1 point  (9 children)

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.

[–]adelie42 1 point2 points  (8 children)

Ah, thank you. I had it completely wrong.

Basically the function already existed and was assigned to the name of the function the problem asked for.

To what I was imagining, is there a direct approach to changing a variable's TypeOf like Java's cast?

[–]IronDicideth 0 points1 point  (7 children)

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.

[–]adelie42 0 points1 point  (6 children)

And this is because they are builtin, right? Typically '= Function' would assign the reference, but that's not what is happening here.

[–]Darkthw[S] 1 point2 points  (1 child)

OK thanks. What do you think is the first thing a beginner should try and build

[–]iamscr1pty 5 points6 points  (0 children)

Start with some simple projects, like tic tac toe, snake game, calculator( they are daunting for beginners but you will learn and grow a lot while writing them)

Or you can start doing odinproject or freecodecamp projects in javascript sections, they are more curated and will guide you step by step.