Skip to content

Improve Concept Exercise: Type Conversion #1248

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged

Conversation

junedev
Copy link
Member

@junedev junedev commented Aug 3, 2021

Closes #1247
Closes #947 (@SleeplessByte I diverged a bit from what you wrote there, hope that's ok for now.)

Notes

  • I don't cover the "behind the scenes" currently in about.md, e.g. boxed vs. non-boxed primitives, toPrimitive, details on toString. This is to save time right now but also because it is not clear to me yet whats the best place for this info in our concept tree.
  • I also don't cover all the weirdness currently, like object + string. Not sure how much value there is going into this tbh.
  • I replaced the third task because the old one didn't practice anything that wasn't already part of task 1 and 2. The new task now practices implicit conversion to boolean.
  • There are lots of places where I use MDN links currently but once we have more concepts of our own (e.g. on JSON) we could link to those.
  • No analyzer ideas yet in the design.md file, also to save some time before launch.

@junedev junedev added x:action/improve Improve existing functionality/content x:knowledge/intermediate Quite a bit of Exercism knowledge required x:module/concept Work on Concepts x:module/concept-exercise Work on Concept Exercises x:size/large Large amount of work x:type/content Work on content (e.g. exercises, concepts) labels Aug 21, 2021
@junedev junedev marked this pull request as ready for review August 21, 2021 13:02
@junedev junedev requested a review from a team August 21, 2021 13:03
@angelikatyborska
Copy link
Member

FYI the PR description references this PR with "closes", I think you meant to reference the issue #1247 instead?

@junedev
Copy link
Member Author

junedev commented Aug 22, 2021

Yes. 🙈 Fixed.

Copy link
Member

@angelikatyborska angelikatyborska left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I read through everything and tried to solve the exercise in my head before looking up the hints. It all makes sense to me, I didn't find any problems. And I learned something!

Comment on lines 40 to 44
Boolean('0');
// => true

Boolean('false');
// => true
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not much of a Polyglot but I'm curious which languages would consider those strings to be falsy?

In Ruby and Elixir, the concept of falsy is very limited, only 0 and nil are falsy.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

On this page https://javascript.info/type-conversions#boolean-conversion they say '0' is falsy in PHP. 'false' is not really an example from another language, it is rather something that might be unexpected. I run into this last week when I wrote if (<some environment variable>). Then I was confused why the if block was still executed even though the environment variable was set to false. Anyway, I will use the phrasing from the introduction instead so people don't start wondering about which languages are meant here.

Comment on lines +92 to +93
Number(undefined);
// => NaN
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

So Number(undefined) is NaN, but Number() is 0? 🤯

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

you are right but I didn't know this either 🤯

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I added a sentence about this behavior. I couldn't find an explanation though.
@SleeplessByte Do you know what's going on there?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I do. It's specced

Number ( [ value ] )

Returns a Number value (not a Number object) computed by ToNumber(value) if value was supplied, else returns +0.

So this has a special case for no argument, which is +0.

Then we check ToNumber:

Argument Type Result
Undefined NaN
Null +0
Boolean The result is 1 if the argument is true. The result is +0 if the argument is false.
Number The result equals the input argument (no conversion).
String See grammar and note below.
Object Apply the following steps:Let primValue be ToPrimitive(input argument, hint Number).Return ToNumber(primValue).

and thus explicitly passing undefined is a different result (NaN) than passing nothing.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I got that part but I was wondering why it was specced that way.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Probably legacy / new String, new Array, new Number should be "the same" as in create an "empty" value of that type ('', [], and +0). It would be weird if that created a NaN. However passing in undefined makes it subject to coercion. Something like that.

Comment on lines 146 to 158
Another common way to achieve a better string representation for objects and arrays is to use [JSON encoding][json].

```javascript
const obj = {
name: 'Gilda Guerra',
address: {
city: 'Goiânia',
},
};

JSON.stringify(obj);
// => '{"name":"Gilda Guerra","address":{"city":"Goiânia"}}'
```
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should this text also mention JSON.parse?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I didn't want to dive too much into this because it will have a separate exercise/concept but I agree that if I show the one direction I should also mention the other way. I added a sentence to cover that.


### String Context

If the addition operator `+` is used for primitive values and one operand is a string, the other one will be coerced into a string as well (if necessary).
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think the "if necessary" is already implied by saying "one operand is a string". If only one of the is a string, it is necessary.

Suggested change
If the addition operator `+` is used for primitive values and one operand is a string, the other one will be coerced into a string as well (if necessary).
If the addition operator `+` is used for primitive values and one operand is a string, the other one will be coerced into a string as well.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

fixed in all the places

@@ -1,6 +1,6 @@
{
"blurb": "TODO: add blurb for lucky-numbers exercise",
"authors": ["shubhsk88"],
"blurb": "Practice type conversion while helping your friend Kojo with his website.",
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I know blurbs are like "whatever", but... this one feels a bit weird to me because it doesn't have any connection to the exercise's name. Maybe:

Suggested change
"blurb": "Practice type conversion while helping your friend Kojo with his website.",
"blurb": "Practice type conversion while helping your friend Kojo with his website www.fun-with-numbers.com.",

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

fixed, I do think blurbs are important

@junedev
Copy link
Member Author

junedev commented Aug 22, 2021

@SleeplessByte I will merge this now. Feel free to also do another review here later if you want and I will create a PR with the fixes.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
x:action/improve Improve existing functionality/content x:knowledge/intermediate Quite a bit of Exercism knowledge required x:module/concept Work on Concepts x:module/concept-exercise Work on Concept Exercises x:size/large Large amount of work x:type/content Work on content (e.g. exercises, concepts)
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Improve Concept Exercise: Type Conversion (lucky numbers) [V3] Implement new Concept Exercise: type-conversion
3 participants