Skip to content
Merged
Show file tree
Hide file tree
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 Jun 26, 2025
22bd128
Update config.json
quintuple-mallard Jun 26, 2025
9704ea2
Update config.json
quintuple-mallard Jun 26, 2025
0b981f0
Update config.json
quintuple-mallard Jun 26, 2025
bbbb3cf
Update config.json
quintuple-mallard Jun 26, 2025
3895060
Update config.json
quintuple-mallard Jun 26, 2025
5b4165f
Update config.json
quintuple-mallard Jun 26, 2025
a31d9d5
Update config.json
quintuple-mallard Jun 26, 2025
0c39de2
Create hints.md
quintuple-mallard Jun 26, 2025
15f1f7d
Create config.json
quintuple-mallard Jun 26, 2025
e4628e3
Rename concepts/type-checking/config.json to concepts/type-checking/.…
quintuple-mallard Jun 26, 2025
fac4100
Create about.md
quintuple-mallard Jun 26, 2025
8b4cdd2
Update about.md
quintuple-mallard Jun 26, 2025
6468833
Format 2 files
quintuple-mallard Jun 26, 2025
bae868a
Update about.md
quintuple-mallard Jun 26, 2025
8f7d4e8
Update about.md
quintuple-mallard Jun 26, 2025
1cba79a
Format
quintuple-mallard Jun 26, 2025
1bc0bcf
Create introduction.md
quintuple-mallard Jun 27, 2025
3ade676
Create links.json
quintuple-mallard Jun 27, 2025
5086219
Update config.json
quintuple-mallard Jun 27, 2025
5af6d69
Update introduction.md
quintuple-mallard Jun 27, 2025
8f74707
Update instructions.md
quintuple-mallard Jun 27, 2025
22c9e25
Update instructions.md
quintuple-mallard Jun 27, 2025
728020d
Node.js magic
quintuple-mallard Jun 27, 2025
5a7d153
Update package.json
quintuple-mallard Jun 27, 2025
d9a7f8b
Update instructions.md
quintuple-mallard Jun 27, 2025
a1c7fc0
Update instructions.md
quintuple-mallard Jun 27, 2025
595ecba
Update instructions.md
quintuple-mallard Jun 27, 2025
90b0cd3
Update assembly-line.js
quintuple-mallard Jun 27, 2025
55e7611
Update assembly-line.js
quintuple-mallard Jun 27, 2025
a27ef2b
Update assembly-line.js
quintuple-mallard Jun 27, 2025
aced68a
Update instructions.md
quintuple-mallard Jun 27, 2025
e38bb7a
Update assembly-line.js
quintuple-mallard Jun 27, 2025
24bad9d
Update exemplar.js
quintuple-mallard Jun 27, 2025
8ccb0c5
Update exemplar.js
quintuple-mallard Jun 28, 2025
57b21c9
Update exemplar.js
quintuple-mallard Jun 28, 2025
f0c9186
Update exemplar.js
quintuple-mallard Jun 28, 2025
8020cfa
Update introduction.md
quintuple-mallard Jun 28, 2025
50e4e94
Update introduction.md
quintuple-mallard Jun 28, 2025
3cdcf38
Update about.md
quintuple-mallard Jun 28, 2025
cee3bb3
Update assembly-line.spec.js
quintuple-mallard Jun 28, 2025
ef5ff73
Update assembly-line.spec.js
quintuple-mallard Jun 28, 2025
f7bfcf4
Update assembly-line.spec.js
quintuple-mallard Jun 28, 2025
1bd721b
Add tests for isNumber
quintuple-mallard Jun 28, 2025
4f586e1
Update assembly-line.spec.js
quintuple-mallard Jun 28, 2025
988aafc
Update assembly-line.spec.js
quintuple-mallard Jun 28, 2025
ef8e430
Update exemplar.js
quintuple-mallard Jun 28, 2025
c5b339c
Update exemplar.js
quintuple-mallard Jun 28, 2025
b1977ad
Update exemplar.js
quintuple-mallard Jun 28, 2025
4057c80
Update assembly-line.spec.js
quintuple-mallard Jun 28, 2025
dbcc9cd
Update assembly-line.spec.js
quintuple-mallard Jun 28, 2025
8185213
Update assembly-line.spec.js
quintuple-mallard Jun 28, 2025
895163c
Update assembly-line.spec.js
quintuple-mallard Jun 28, 2025
0033bd6
Update assembly-line.spec.js
quintuple-mallard Jun 28, 2025
0d884ac
Update exemplar.js
quintuple-mallard Jun 28, 2025
265ea06
Update exemplar.js
quintuple-mallard Jun 28, 2025
7f8b489
Update exemplar.js
quintuple-mallard Jun 28, 2025
b887fd5
Update exemplar.js
quintuple-mallard Jun 28, 2025
84b94b1
Update exemplar.js
quintuple-mallard Jun 28, 2025
1ce83b2
Update exemplar.js
quintuple-mallard Jun 28, 2025
6bedc11
Update assembly-line.js
quintuple-mallard Jun 28, 2025
261e932
Update assembly-line.spec.js
quintuple-mallard Jun 28, 2025
c2fd423
Update about.md
quintuple-mallard Jun 29, 2025
befc340
Update introduction.md
quintuple-mallard Jun 29, 2025
586bac3
Update about.md
quintuple-mallard Jun 29, 2025
033a779
Update introduction.md
quintuple-mallard Jun 29, 2025
0e8901d
Update assembly-line.spec.js
quintuple-mallard Jun 29, 2025
7e5b49d
Update assembly-line.spec.js
quintuple-mallard Jun 29, 2025
55d78f6
Update assembly-line.spec.js
quintuple-mallard Jun 29, 2025
7b1bb6a
Update exemplar.js
quintuple-mallard Jun 29, 2025
d561f40
Update assembly-line.spec.js
quintuple-mallard Jun 29, 2025
c43ad40
Merge branch 'exercism:main' into main
quintuple-mallard Jun 29, 2025
b2051de
Update assembly-line.spec.js
quintuple-mallard Jun 29, 2025
6283942
Update hints.md
quintuple-mallard Jun 29, 2025
f2ce44c
Update hints.md
quintuple-mallard Jun 29, 2025
7b01042
Update hints.md
quintuple-mallard Jun 30, 2025
b906c42
Update instructions.md
quintuple-mallard Jun 30, 2025
30915cf
Update assembly-line.spec.js
quintuple-mallard Jun 30, 2025
5abc5c3
Update instructions.md
quintuple-mallard Jun 30, 2025
68f8402
Update instructions.md
quintuple-mallard Jun 30, 2025
d501307
Update assembly-line.js
quintuple-mallard Jun 30, 2025
3109d6d
Update exemplar.js
quintuple-mallard Jun 30, 2025
45d7407
Update exemplar.js
quintuple-mallard Jun 30, 2025
bbfc4a2
Update assembly-line.spec.js
quintuple-mallard Jun 30, 2025
a0f87e6
Update config.json
quintuple-mallard Jun 30, 2025
f6ad93f
rename Assembly Line to Recycling Robot
quintuple-mallard Jun 30, 2025
48996bd
Merge pull request #1 from quintuple-mallard/add-assembly-line
quintuple-mallard Jun 30, 2025
d6631bf
Fix typo in instructions.md
quintuple-mallard Jul 1, 2025
326bbdb
Update hints.md
quintuple-mallard Jul 1, 2025
511e5a0
Format
quintuple-mallard Jul 1, 2025
e8fb4e9
Update instructions.md
quintuple-mallard Jul 1, 2025
2007b10
Update instructions.md
quintuple-mallard Jul 1, 2025
246ac06
Update concepts/type-checking/.meta/config.json
quintuple-mallard Jul 10, 2025
fab8038
Update concepts/type-checking/about.md
quintuple-mallard Jul 10, 2025
d40bada
Update concepts/type-checking/about.md
quintuple-mallard Jul 10, 2025
88486ec
Update concepts/type-checking/about.md
quintuple-mallard Jul 10, 2025
a43d16b
Update about.md
quintuple-mallard Jul 10, 2025
76f0914
Update introduction.md
quintuple-mallard Jul 10, 2025
ed35aa7
Update introduction.md
quintuple-mallard Jul 10, 2025
8011018
Format
quintuple-mallard Jul 10, 2025
f6b496f
Update assembly-line.js
quintuple-mallard Jul 10, 2025
7742dcf
Update exemplar.js
quintuple-mallard Jul 10, 2025
f938fa0
Update exemplar.js
quintuple-mallard Jul 10, 2025
4544ce0
Update package.json
quintuple-mallard Jul 10, 2025
c040040
Merge branch 'exercism:main' into main
quintuple-mallard Jul 10, 2025
31e8328
Update exemplar.js
quintuple-mallard Jul 10, 2025
d8e48bc
Revert "Format"
quintuple-mallard Jul 10, 2025
2742e1d
Format
quintuple-mallard Jul 10, 2025
2c8f19c
Merge branch 'main' of https://github.com/quintuple-mallard/javascript
quintuple-mallard Jul 10, 2025
2c0358c
Update concepts/type-checking/about.md
quintuple-mallard Jul 11, 2025
44072bd
Update concepts/type-checking/about.md
quintuple-mallard Jul 11, 2025
6f0f146
Update concepts/type-checking/about.md
quintuple-mallard Jul 11, 2025
faa6fea
Update concepts/type-checking/about.md
quintuple-mallard Jul 11, 2025
d41997d
Update about.md
quintuple-mallard Jul 11, 2025
d5c4fb0
Update about.md
quintuple-mallard Jul 11, 2025
c44db51
Update concepts/type-checking/about.md
quintuple-mallard Jul 11, 2025
0c4d442
Update concepts/type-checking/about.md
quintuple-mallard Jul 11, 2025
7780f5c
Update concepts/type-checking/about.md
quintuple-mallard Jul 11, 2025
88da8ba
Update concepts/type-checking/about.md
quintuple-mallard Jul 11, 2025
803f3c1
Update concepts/type-checking/about.md
quintuple-mallard Jul 11, 2025
fccfddc
Update concepts/type-checking/about.md
quintuple-mallard Jul 11, 2025
741916f
Update introduction.md
quintuple-mallard Jul 11, 2025
ae0ef2e
Update exercises/concept/recycling-robot/.docs/instructions.md
quintuple-mallard Jul 11, 2025
49da298
Update exercises/concept/recycling-robot/.docs/instructions.md
quintuple-mallard Jul 11, 2025
e078be7
Update instructions.md
quintuple-mallard Jul 11, 2025
0c832fe
Update instructions.md
quintuple-mallard Jul 11, 2025
4e50a01
Update instructions.md
quintuple-mallard Jul 11, 2025
62682d5
Update exercises/concept/recycling-robot/.docs/instructions.md
quintuple-mallard Jul 11, 2025
712c34f
Update exercises/concept/recycling-robot/.docs/instructions.md
quintuple-mallard Jul 11, 2025
a0f5dff
Update exercises/concept/recycling-robot/.docs/instructions.md
quintuple-mallard Jul 11, 2025
c820258
Update exercises/concept/recycling-robot/.docs/instructions.md
quintuple-mallard Jul 11, 2025
784a2ae
Update config.json
quintuple-mallard Jul 11, 2025
61efdc4
Update exercises/concept/recycling-robot/.docs/introduction.md
quintuple-mallard Jul 11, 2025
97a5c22
Update introduction.md
quintuple-mallard Jul 11, 2025
5608f6b
Update exemplar.js
quintuple-mallard Jul 11, 2025
a5cd0c1
Update config.json
quintuple-mallard Jul 11, 2025
64a95aa
Update exemplar.js
quintuple-mallard Jul 11, 2025
5dda1a0
Update assembly-line.js
quintuple-mallard Jul 11, 2025
9fa8ebc
Create lib.js
quintuple-mallard Jul 11, 2025
63375b1
Update assembly-line.js
quintuple-mallard Jul 11, 2025
7a945a6
Update assembly-line.spec.js
quintuple-mallard Jul 11, 2025
e28b1ed
Update exemplar.js
quintuple-mallard Jul 11, 2025
750c967
Update exemplar.js
quintuple-mallard Jul 11, 2025
5176daa
Configlet format
quintuple-mallard Jul 11, 2025
1fe14cf
nodejs format
quintuple-mallard Jul 11, 2025
c46d980
Update config.json
quintuple-mallard Jul 11, 2025
23355e6
Update config.json
quintuple-mallard Jul 11, 2025
12fe19a
Apply suggestions from code review
SleeplessByte Jul 11, 2025
b4c172d
Update concepts/type-checking/about.md
SleeplessByte Jul 11, 2025
ff4f08f
Format
SleeplessByte Jul 11, 2025
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions concepts/type-checking/.meta/config.json
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": []
}
164 changes: 164 additions & 0 deletions concepts/type-checking/about.md
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.

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
164 changes: 164 additions & 0 deletions concepts/type-checking/introduction.md
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.

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
14 changes: 14 additions & 0 deletions concepts/type-checking/links.json
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"
}
]
21 changes: 21 additions & 0 deletions config.json
Original file line number Diff line number Diff line change
Expand Up @@ -436,6 +436,22 @@
"objects",
"functions"
]
},
{
"slug": "recycling-robot",
"name": "Recycling Robot",
"uuid": "16114449-52fe-470e-af11-cf9dc3689a93",
"concepts": [
"type-checking"
],
"prerequisites": [
"basics",
"errors",
"objects",
"arrays",
"classes",
"inheritance"
]
}
],
"practice": [
Expand Down Expand Up @@ -2887,6 +2903,11 @@
"uuid": "ca322d6f-0f7e-4a2d-a058-e98a59cdae93",
"slug": "randomness",
"name": "Randomness"
},
{
"uuid": "72e51fbe-db98-492e-b155-8ef21623f741",
"slug": "type-checking",
"name": "Type Checking"
}
],
"key_features": [
Expand Down
Loading