Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
6 changes: 3 additions & 3 deletions exercises/concept/recycling-robot/.docs/hints.md
Original file line number Diff line number Diff line change
Expand Up @@ -40,14 +40,14 @@
- `typeof` returns a string.
- You can check the length of an array to find out how many elements it contains.

## 8. Throw an error if an object does not have the `id` property or method
## 8. Check if an object has a `type` property or method

- You can use the `in` operator to check if an object has a property or method.
- If the `id` property or method is missing, your function should throw an `Error`.

## 9. Check if an object has a `type` property or method
## 9. Throw an error if an object does not have the `id` property or method

- You can use the `in` operator to check if an object has a property or method.
- If the `id` property or method is missing, your function should throw an `Error`.

## 10. Check if an object has an `id` property

Expand Down
53 changes: 31 additions & 22 deletions exercises/concept/recycling-robot/.docs/instructions.md
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,8 @@ isObject(25n);

### 4. Check if a string is numeric

Implement the `isNumericString` function, that should check if the value is a string that only consists of digits.
Implement the `isNumericString` function, that should check if the value is a string that only consists of digits or a minus followed by digits indicating a negative number.
Only integers should be considered, decimals are not considered numeric for this check of the recycling robot.

```javascript
isNumericString(42);
Expand Down Expand Up @@ -109,21 +110,7 @@ isEmptyArray([]);
// => true
```

### 8. Throw an error if an object does not have an `id` property or method

Implement the `assertHasId` function, that will throw an `Error` if an object is missing the `id` property.

If an object does have the `id` property, it should not return anything.

```javascript
assertHasId({ id: 42, color: 'red' });
// => undefined

assertHasId({ color: 'green' });
// Error: "Object is missing the 'id' property"
```

### 9. Check if an object has a `type` property or method
### 8. Check if an object has a `type` property or method

Implement the `hasType` function, that checks whether an object has a `type` property or method.

Expand All @@ -133,40 +120,62 @@ class Keyboard(){
// ...
}
}
hasType({type:"car",color:"red"})
hasType({ type:"car", color:"red" })
// => true

hasType({color:"green"})
hasType({ color:"green" })
// => false

hasType(new Keyboard())
// => true
```

### 9. Throw an error if an object does not have an `id` property or method

Implement the `assertHasId` function, that will throw an `Error` if an object is missing the `id` property.

If an object does have the `id` property, it should not return anything.

```javascript
assertHasId({ id: 42, color: 'red' });
// => undefined

assertHasId({ color: 'green' });
// Error: "Object is missing the 'id' property"
```

### 10. Check if an object has an `id` property

Implement the `hasIdProperty` function, that checks whether an object has an `id` property.

```javascript
class MyClass {
class SimpleData {
constructor() {
this.number = '42';
this.id = 'BC269327FE1D9B95';
}
}
class MyNewClass {

class StealingData extends SimpleData {}

class MethodData {
constructor() {
this.number = '42';
this._id = 'BC269327FE1D9B95';
}

get id() {
return this._id;
}
}
hasIdProperty(new MyClass());

hasIdProperty(new SimpleData());
// => true

hasIdProperty(new MyNewClass());
hasIdProperty(new MethodData());
// => false

hasIdProperty(new StealingData());
// => false
```

Expand Down
34 changes: 14 additions & 20 deletions exercises/concept/recycling-robot/.meta/exemplar.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,7 @@ export function isBoolean(value) {
*/
export function isNumber(value) {
return (
(typeof value === 'number' || typeof value === 'bigint') &&
!isNaN(Number(value)) &&
value !== Infinity
(typeof value === 'number' && isFinite(value)) || typeof value === 'bigint'
);
}

Expand All @@ -47,12 +45,7 @@ export function isObject(value) {
* @returns {boolean} whether the input is a numeric string.
*/
export function isNumericString(value) {
return (
typeof value === 'string' &&
value.split('').every((char) => {
return /[0-9]/.test(char);
})
);
return typeof value === 'string' && /^-?\d+$/.test(value);
}

/**
Expand Down Expand Up @@ -86,26 +79,27 @@ export function isEmptyArray(value) {
}

/**
* Throws an error if an object is missing an "id" property or method.
* Checks if a value has a "type" property or method.
*
* @param {object} object
* @returns {boolean} undefined if the input has an "id" property, otherwise throws an error.
* @returns {boolean} whether the input has a "type" property.
*/
export function assertHasId(object) {
if ('id' in object) {
return;
}
throw new Error('The "id" property is missing.');
export function hasType(object) {
return 'type' in object;
}

/**
* Checks if a value has a "type" property or method.
* Throws an error if an object is missing an "id" property or method.
*
* @param {object} object
* @returns {boolean} whether the input has a "type" property.
* @returns {never|void} undefined if the input has an "id" property, otherwise throws an error.
*/
export function hasType(object) {
return 'type' in object;
export function assertHasId(object) {
if ('id' in object) {
return;
}

throw new Error('The "id" property is missing.');
}

/**
Expand Down
16 changes: 8 additions & 8 deletions exercises/concept/recycling-robot/assembly-line.js
Original file line number Diff line number Diff line change
Expand Up @@ -81,23 +81,23 @@ export function isEmptyArray(value) {
}

/**
* Throws an error if an object is missing an "id" property or method.
* Checks if a value has a "type" property or method.
*
* @param {object} object
* @returns {undefined} undefined if the input has an "id" property or method, otherwise throws an error.
* @returns {boolean} whether the input has a "type" property or method.
*/
export function assertHasId(object) {
throw new Error('Remove this line and implement the assertHasId function');
export function hasType(object) {
throw new Error('Remove this line and implement the hasType function');
}

/**
* Checks if a value has a "type" property or method.
* Throws an error if an object is missing an "id" property or method.
*
* @param {object} object
* @returns {boolean} whether the input has a "type" property or method.
* @returns {never|void} undefined if the input has an "id" property or method, otherwise throws an error.
*/
export function hasType(object) {
throw new Error('Remove this line and implement the hasType function');
export function assertHasId(object) {
throw new Error('Remove this line and implement the assertHasId function');
}

/**
Expand Down
Loading