-
-
Notifications
You must be signed in to change notification settings - Fork 637
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
Improve Concept Exercise: Type Conversion #1248
Conversation
FYI the PR description references this PR with "closes", I think you meant to reference the issue #1247 instead? |
Yes. 🙈 Fixed. |
There was a problem hiding this 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!
concepts/type-conversion/about.md
Outdated
Boolean('0'); | ||
// => true | ||
|
||
Boolean('false'); | ||
// => true |
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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.
Number(undefined); | ||
// => NaN |
There was a problem hiding this comment.
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
? 🤯
There was a problem hiding this comment.
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 🤯
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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.
concepts/type-conversion/about.md
Outdated
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"}}' | ||
``` |
There was a problem hiding this comment.
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
?
There was a problem hiding this comment.
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.
concepts/type-conversion/about.md
Outdated
|
||
### 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). |
There was a problem hiding this comment.
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.
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. |
There was a problem hiding this comment.
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.", |
There was a problem hiding this comment.
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:
"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.", |
There was a problem hiding this comment.
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
@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. |
Closes #1247
Closes #947 (@SleeplessByte I diverged a bit from what you wrote there, hope that's ok for now.)
Notes