-
-
Notifications
You must be signed in to change notification settings - Fork 655
Add Concept Exercise for type checking. #2704
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
Merged
Changes from all commits
Commits
Show all changes
150 commits
Select commit
Hold shift + click to select a range
95249ec
Create assembly-line
quintuple-mallard 22bd128
Update config.json
quintuple-mallard 9704ea2
Update config.json
quintuple-mallard 0b981f0
Update config.json
quintuple-mallard bbbb3cf
Update config.json
quintuple-mallard 3895060
Update config.json
quintuple-mallard 5b4165f
Update config.json
quintuple-mallard a31d9d5
Update config.json
quintuple-mallard 0c39de2
Create hints.md
quintuple-mallard 15f1f7d
Create config.json
quintuple-mallard e4628e3
Rename concepts/type-checking/config.json to concepts/type-checking/.…
quintuple-mallard fac4100
Create about.md
quintuple-mallard 8b4cdd2
Update about.md
quintuple-mallard 6468833
Format 2 files
quintuple-mallard bae868a
Update about.md
quintuple-mallard 8f7d4e8
Update about.md
quintuple-mallard 1cba79a
Format
quintuple-mallard 1bc0bcf
Create introduction.md
quintuple-mallard 3ade676
Create links.json
quintuple-mallard 5086219
Update config.json
quintuple-mallard 5af6d69
Update introduction.md
quintuple-mallard 8f74707
Update instructions.md
quintuple-mallard 22c9e25
Update instructions.md
quintuple-mallard 728020d
Node.js magic
quintuple-mallard 5a7d153
Update package.json
quintuple-mallard d9a7f8b
Update instructions.md
quintuple-mallard a1c7fc0
Update instructions.md
quintuple-mallard 595ecba
Update instructions.md
quintuple-mallard 90b0cd3
Update assembly-line.js
quintuple-mallard 55e7611
Update assembly-line.js
quintuple-mallard a27ef2b
Update assembly-line.js
quintuple-mallard aced68a
Update instructions.md
quintuple-mallard e38bb7a
Update assembly-line.js
quintuple-mallard 24bad9d
Update exemplar.js
quintuple-mallard 8ccb0c5
Update exemplar.js
quintuple-mallard 57b21c9
Update exemplar.js
quintuple-mallard f0c9186
Update exemplar.js
quintuple-mallard 8020cfa
Update introduction.md
quintuple-mallard 50e4e94
Update introduction.md
quintuple-mallard 3cdcf38
Update about.md
quintuple-mallard cee3bb3
Update assembly-line.spec.js
quintuple-mallard ef5ff73
Update assembly-line.spec.js
quintuple-mallard f7bfcf4
Update assembly-line.spec.js
quintuple-mallard 1bd721b
Add tests for isNumber
quintuple-mallard 4f586e1
Update assembly-line.spec.js
quintuple-mallard 988aafc
Update assembly-line.spec.js
quintuple-mallard ef8e430
Update exemplar.js
quintuple-mallard c5b339c
Update exemplar.js
quintuple-mallard b1977ad
Update exemplar.js
quintuple-mallard 4057c80
Update assembly-line.spec.js
quintuple-mallard dbcc9cd
Update assembly-line.spec.js
quintuple-mallard 8185213
Update assembly-line.spec.js
quintuple-mallard 895163c
Update assembly-line.spec.js
quintuple-mallard 0033bd6
Update assembly-line.spec.js
quintuple-mallard 0d884ac
Update exemplar.js
quintuple-mallard 265ea06
Update exemplar.js
quintuple-mallard 7f8b489
Update exemplar.js
quintuple-mallard b887fd5
Update exemplar.js
quintuple-mallard 84b94b1
Update exemplar.js
quintuple-mallard 1ce83b2
Update exemplar.js
quintuple-mallard 6bedc11
Update assembly-line.js
quintuple-mallard 261e932
Update assembly-line.spec.js
quintuple-mallard c2fd423
Update about.md
quintuple-mallard befc340
Update introduction.md
quintuple-mallard 586bac3
Update about.md
quintuple-mallard 033a779
Update introduction.md
quintuple-mallard 0e8901d
Update assembly-line.spec.js
quintuple-mallard 7e5b49d
Update assembly-line.spec.js
quintuple-mallard 55d78f6
Update assembly-line.spec.js
quintuple-mallard 7b1bb6a
Update exemplar.js
quintuple-mallard d561f40
Update assembly-line.spec.js
quintuple-mallard c43ad40
Merge branch 'exercism:main' into main
quintuple-mallard b2051de
Update assembly-line.spec.js
quintuple-mallard 6283942
Update hints.md
quintuple-mallard f2ce44c
Update hints.md
quintuple-mallard 7b01042
Update hints.md
quintuple-mallard b906c42
Update instructions.md
quintuple-mallard 30915cf
Update assembly-line.spec.js
quintuple-mallard 5abc5c3
Update instructions.md
quintuple-mallard 68f8402
Update instructions.md
quintuple-mallard d501307
Update assembly-line.js
quintuple-mallard 3109d6d
Update exemplar.js
quintuple-mallard 45d7407
Update exemplar.js
quintuple-mallard bbfc4a2
Update assembly-line.spec.js
quintuple-mallard a0f87e6
Update config.json
quintuple-mallard f6ad93f
rename Assembly Line to Recycling Robot
quintuple-mallard 48996bd
Merge pull request #1 from quintuple-mallard/add-assembly-line
quintuple-mallard d6631bf
Fix typo in instructions.md
quintuple-mallard 326bbdb
Update hints.md
quintuple-mallard 511e5a0
Format
quintuple-mallard e8fb4e9
Update instructions.md
quintuple-mallard 2007b10
Update instructions.md
quintuple-mallard 246ac06
Update concepts/type-checking/.meta/config.json
quintuple-mallard fab8038
Update concepts/type-checking/about.md
quintuple-mallard d40bada
Update concepts/type-checking/about.md
quintuple-mallard 88486ec
Update concepts/type-checking/about.md
quintuple-mallard a43d16b
Update about.md
quintuple-mallard 76f0914
Update introduction.md
quintuple-mallard ed35aa7
Update introduction.md
quintuple-mallard 8011018
Format
quintuple-mallard f6b496f
Update assembly-line.js
quintuple-mallard 7742dcf
Update exemplar.js
quintuple-mallard f938fa0
Update exemplar.js
quintuple-mallard 4544ce0
Update package.json
quintuple-mallard c040040
Merge branch 'exercism:main' into main
quintuple-mallard 31e8328
Update exemplar.js
quintuple-mallard d8e48bc
Revert "Format"
quintuple-mallard 2742e1d
Format
quintuple-mallard 2c8f19c
Merge branch 'main' of https://github.com/quintuple-mallard/javascript
quintuple-mallard 2c0358c
Update concepts/type-checking/about.md
quintuple-mallard 44072bd
Update concepts/type-checking/about.md
quintuple-mallard 6f0f146
Update concepts/type-checking/about.md
quintuple-mallard faa6fea
Update concepts/type-checking/about.md
quintuple-mallard d41997d
Update about.md
quintuple-mallard d5c4fb0
Update about.md
quintuple-mallard c44db51
Update concepts/type-checking/about.md
quintuple-mallard 0c4d442
Update concepts/type-checking/about.md
quintuple-mallard 7780f5c
Update concepts/type-checking/about.md
quintuple-mallard 88da8ba
Update concepts/type-checking/about.md
quintuple-mallard 803f3c1
Update concepts/type-checking/about.md
quintuple-mallard fccfddc
Update concepts/type-checking/about.md
quintuple-mallard 741916f
Update introduction.md
quintuple-mallard ae0ef2e
Update exercises/concept/recycling-robot/.docs/instructions.md
quintuple-mallard 49da298
Update exercises/concept/recycling-robot/.docs/instructions.md
quintuple-mallard e078be7
Update instructions.md
quintuple-mallard 0c832fe
Update instructions.md
quintuple-mallard 4e50a01
Update instructions.md
quintuple-mallard 62682d5
Update exercises/concept/recycling-robot/.docs/instructions.md
quintuple-mallard 712c34f
Update exercises/concept/recycling-robot/.docs/instructions.md
quintuple-mallard a0f5dff
Update exercises/concept/recycling-robot/.docs/instructions.md
quintuple-mallard c820258
Update exercises/concept/recycling-robot/.docs/instructions.md
quintuple-mallard 784a2ae
Update config.json
quintuple-mallard 61efdc4
Update exercises/concept/recycling-robot/.docs/introduction.md
quintuple-mallard 97a5c22
Update introduction.md
quintuple-mallard 5608f6b
Update exemplar.js
quintuple-mallard a5cd0c1
Update config.json
quintuple-mallard 64a95aa
Update exemplar.js
quintuple-mallard 5dda1a0
Update assembly-line.js
quintuple-mallard 9fa8ebc
Create lib.js
quintuple-mallard 63375b1
Update assembly-line.js
quintuple-mallard 7a945a6
Update assembly-line.spec.js
quintuple-mallard e28b1ed
Update exemplar.js
quintuple-mallard 750c967
Update exemplar.js
quintuple-mallard 5176daa
Configlet format
quintuple-mallard 1fe14cf
nodejs format
quintuple-mallard c46d980
Update config.json
quintuple-mallard 23355e6
Update config.json
quintuple-mallard 12fe19a
Apply suggestions from code review
SleeplessByte b4c172d
Update concepts/type-checking/about.md
SleeplessByte ff4f08f
Format
SleeplessByte File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,8 @@ | ||
| { | ||
| "blurb": "Learn how to check the type of a value or object in JavaScript", | ||
| "authors": [ | ||
| "quintuple-mallard", | ||
| "SleeplessByte" | ||
| ], | ||
| "contributors": [] | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,164 @@ | ||
| # About | ||
|
|
||
| Knowning what the type of a piece of data is, is often very important for code to run smoothly and without errors. | ||
|
|
||
| Javascript has several ways to check the type of a value or object. | ||
|
|
||
| ```exercism/note | ||
| Javascript's type checking mechanisms can be somewhat unreliable. | ||
|
|
||
| For better type safety and stronger types, you should probably use TypeScript, a language that builds on JavaScript, but with the type syntax of a static-typed language. | ||
| ``` | ||
|
|
||
| ## The `typeof` operator | ||
|
|
||
| The `typeof` operator returns the type of its operand. | ||
| The output is a string matching the name of one of the [primitive data types][primitives], except for `"null"`. | ||
| It can also be `"function"` or `"object"`. | ||
|
|
||
| ```javascript | ||
| typeof undefined; | ||
| // => "undefined" | ||
|
|
||
| typeof true; | ||
| // => "boolean" | ||
|
|
||
| typeof 42; | ||
| // => "number" | ||
|
|
||
| typeof 'Hello, World!'; | ||
| // => "string" | ||
|
|
||
| typeof function () { | ||
| return 'Hello, World'; | ||
| }; | ||
| // => "function" | ||
|
|
||
| typeof [1, 2, 3, 4]; | ||
| // => "object" | ||
|
|
||
| typeof { city: 'Stockholm', country: 'Sweden' }; | ||
| // => "object" | ||
| ``` | ||
|
|
||
| For [historical reasons][`typeof null` is `"object"`]. | ||
|
|
||
| ## The `instanceof` operator | ||
|
|
||
| For checking the type of an object, you can use the `instanceof` operator. | ||
| It evaluates into a `boolean` depending on whether the second operand is included in the first operands' [prototype chain][prototype chain]. | ||
| To clarify, `instanceof` will return whether the first operand is an instance of second operand or one of its child classes. | ||
| `instanceof` only works on objects. | ||
|
|
||
| ```javascript | ||
| class Beverage { | ||
| // ... | ||
| } | ||
|
|
||
| // The Coffee class is a child of the Beverage class. | ||
| class Coffee extends Beverage { | ||
| // ... | ||
| } | ||
|
|
||
| const java = new Coffee(); | ||
|
|
||
| java instanceof Coffee; | ||
| // => true | ||
|
|
||
| java instanceof Beverage; | ||
| // => true | ||
| ``` | ||
|
|
||
| ````exercism/advanced | ||
| The `Array` class has a method called `Array.isArray()` that checks if its argument is an array. | ||
|
|
||
| While `instanceof Array` will not work with an array created in a different realm such as an `iframe` in a webpage, `Array.isArray()` will. | ||
|
|
||
| This is because the Array class has a different constructor in each realm, and each `iframe` has its own ream, meaning that the function in the prototype chain will be different, causing `instanceof Array` to fail. | ||
| `Array.isArray()` is capable of ignoring this, and should always be used when possible. | ||
quintuple-mallard marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
|
||
| It can also survive false positives where an object isn't actually an `Array`, and merely has `Array` in its prototype chain. | ||
|
|
||
| ```javascript | ||
| ({ __proto__: Array.prototype }) instanceof Array | ||
| // => true | ||
|
|
||
| Array.isArray({ __proto__: Array.prototype }) | ||
| // => false | ||
| ``` | ||
|
|
||
| ```` | ||
|
|
||
| ## The `in` operator | ||
|
|
||
| The `in` operator returns whether the first operand is a property of the second operand. | ||
| It does not check that the property has a defined value. | ||
| A property set to `undefined` will still be detected by `in`. | ||
|
|
||
| ```javascript | ||
| class Coffee { | ||
| constructor() { | ||
| this.temperature = 'hot'; | ||
| this.isDarkMatter = undefined; | ||
| } | ||
|
|
||
| coolDown() { | ||
| this.temperature = 'warm'; | ||
| } | ||
| } | ||
|
|
||
| const espresso = new Coffee(); | ||
quintuple-mallard marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
|
||
| 'temperature' in espresso; | ||
| // => true | ||
|
|
||
| 'color' in espresso; | ||
| // => false | ||
|
|
||
| 'isDarkMatter' in espresso; | ||
| // => true | ||
| ``` | ||
|
|
||
| ````exercism/note | ||
| `in` will return `true` for inherited properties and methods. | ||
|
|
||
| ```javascript | ||
| "coolDown" in espresso | ||
| // => true | ||
|
|
||
| "constructor" in espresso | ||
| // => true | ||
| ``` | ||
|
|
||
| To avoid this, use `Object.hasOwn()` instead | ||
| ```` | ||
|
|
||
| ## The `Object.hasOwn()` function | ||
|
|
||
| The `Object.hasOwn()` method returns whether the specified object _owns the given property_ (it is not inherited or a method). | ||
|
|
||
| ```javascript | ||
| class Coffee { | ||
| constructor() { | ||
| this.temperature = 'hot'; | ||
| } | ||
|
|
||
| coolDown() { | ||
| this.temperature = 'warm'; | ||
| } | ||
| } | ||
| const cappuccino = new Coffee(); | ||
quintuple-mallard marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
|
||
| Object.hasOwn(cappucino, 'temperature'); | ||
| // => true | ||
|
|
||
| Object.hasOwn(cappucino, 'constructor'); | ||
| // => false | ||
|
|
||
| Object.hasOwn(cappucino, 'coolDown'); | ||
| // => false | ||
| ``` | ||
|
|
||
| [primitives]: https://developer.mozilla.org/en-US/docs/Glossary/Primitive | ||
| [typeof null is object]: https://2ality.com/2013/10/typeof-null.html | ||
| [prototype chain]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Inheritance_and_the_prototype_chain | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,164 @@ | ||
| # About | ||
quintuple-mallard marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
|
||
| Knowning what the type of a piece of data is, is often very important for code to run smoothly and without errors. | ||
|
|
||
| Javascript has several ways to check the type of a value or object. | ||
|
|
||
| ```exercism/note | ||
| Javascript's type checking mechanisms can be somewhat unreliable. | ||
|
|
||
| For better type safety and stronger types, you should probably use TypeScript, a language that builds on JavaScript, but with the type syntax of a static-typed language. | ||
| ``` | ||
|
|
||
| ## The `typeof` operator | ||
|
|
||
| The `typeof` operator returns the type of its operand. | ||
| The output is a string matching the name of one of the [primitive data types][primitives], except for `"null"`. | ||
| It can also be `"function"` or `"object"`. | ||
|
|
||
| ```javascript | ||
| typeof undefined; | ||
| // => "undefined" | ||
|
|
||
| typeof true; | ||
| // => "boolean" | ||
|
|
||
| typeof 42; | ||
| // => "number" | ||
|
|
||
| typeof 'Hello, World!'; | ||
| // => "string" | ||
|
|
||
| typeof function () { | ||
| return 'Hello, World'; | ||
| }; | ||
| // => "function" | ||
|
|
||
| typeof [1, 2, 3, 4]; | ||
| // => "object" | ||
|
|
||
| typeof { city: 'Stockholm', country: 'Sweden' }; | ||
| // => "object" | ||
| ``` | ||
|
|
||
| For [historical reasons][`typeof null` is `"object"`]. | ||
|
|
||
| ## The `instanceof` operator | ||
|
|
||
| For checking the type of an object, you can use the `instanceof` operator. | ||
| It evaluates into a `boolean` depending on whether the second operand is included in the first operands' [prototype chain][prototype chain]. | ||
| To clarify, `instanceof` will return whether the first operand is an instance of second operand or one of its child classes. | ||
| `instanceof` only works on objects. | ||
|
|
||
| ```javascript | ||
| class Beverage { | ||
| // ... | ||
| } | ||
|
|
||
| // The Coffee class is a child of the Beverage class. | ||
| class Coffee extends Beverage { | ||
| // ... | ||
| } | ||
|
|
||
| const java = new Coffee(); | ||
|
|
||
| java instanceof Coffee; | ||
| // => true | ||
|
|
||
| java instanceof Beverage; | ||
| // => true | ||
| ``` | ||
|
|
||
| ````exercism/advanced | ||
| The `Array` class has a method called `Array.isArray()` that checks if its argument is an array. | ||
|
|
||
| While `instanceof Array` will not work with an array created in a different realm such as an `iframe` in a webpage, `Array.isArray()` will. | ||
|
|
||
| This is because the Array class has a different constructor in each realm, and each `iframe` has its own ream, meaning that the function in the prototype chain will be different, causing `instanceof Array` to fail. | ||
| `Array.isArray()` is capable of ignoring this, and should always be used when possible. | ||
|
|
||
| It can also survive false positives where an object isn't actually an `Array`, and merely has `Array` in its prototype chain. | ||
|
|
||
| ```javascript | ||
| ({ __proto__: Array.prototype }) instanceof Array | ||
| // => true | ||
|
|
||
| Array.isArray({ __proto__: Array.prototype }) | ||
| // => false | ||
| ``` | ||
|
|
||
| ```` | ||
|
|
||
| ## The `in` operator | ||
|
|
||
| The `in` operator returns whether the first operand is a property of the second operand. | ||
| It does not check that the property has a defined value. | ||
| A property set to `undefined` will still be detected by `in`. | ||
|
|
||
| ```javascript | ||
| class Coffee { | ||
| constructor() { | ||
| this.temperature = 'hot'; | ||
| this.isDarkMatter = undefined; | ||
| } | ||
|
|
||
| coolDown() { | ||
| this.temperature = 'warm'; | ||
| } | ||
| } | ||
|
|
||
| const espresso = new Coffee(); | ||
|
|
||
| 'temperature' in espresso; | ||
| // => true | ||
|
|
||
| 'color' in espresso; | ||
| // => false | ||
|
|
||
| 'isDarkMatter' in espresso; | ||
| // => true | ||
| ``` | ||
|
|
||
| ````exercism/note | ||
| `in` will return `true` for inherited properties and methods. | ||
|
|
||
| ```javascript | ||
| "coolDown" in espresso | ||
| // => true | ||
|
|
||
| "constructor" in espresso | ||
| // => true | ||
| ``` | ||
|
|
||
| To avoid this, use `Object.hasOwn()` instead | ||
| ```` | ||
|
|
||
| ## The `Object.hasOwn()` function | ||
|
|
||
| The `Object.hasOwn()` method returns whether the specified object _owns the given property_ (it is not inherited or a method). | ||
|
|
||
| ```javascript | ||
| class Coffee { | ||
| constructor() { | ||
| this.temperature = 'hot'; | ||
| } | ||
|
|
||
| coolDown() { | ||
| this.temperature = 'warm'; | ||
| } | ||
| } | ||
| const cappuccino = new Coffee(); | ||
|
|
||
| Object.hasOwn(cappucino, 'temperature'); | ||
| // => true | ||
|
|
||
| Object.hasOwn(cappucino, 'constructor'); | ||
| // => false | ||
|
|
||
| Object.hasOwn(cappucino, 'coolDown'); | ||
| // => false | ||
| ``` | ||
|
|
||
| [primitives]: https://developer.mozilla.org/en-US/docs/Glossary/Primitive | ||
| [typeof null is object]: https://2ality.com/2013/10/typeof-null.html | ||
| [prototype chain]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Inheritance_and_the_prototype_chain | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,14 @@ | ||
| [ | ||
| { | ||
| "url": "https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/typeof", | ||
| "description": "MDN: The typeof operator" | ||
| }, | ||
| { | ||
| "url": "https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/instanceof", | ||
| "description": "MDN: The instanceof operator" | ||
| }, | ||
| { | ||
| "url": "https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/hasOwnProperty", | ||
| "description": "MDN: The object.hasOwnProperty() method" | ||
| } | ||
| ] |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.