Skip to content

Commit c4e4ed8

Browse files
Improve test names and prevent some solutions (#2722)
* Improve test names and prevent some solutions * Fix the tests
1 parent fa81c28 commit c4e4ed8

File tree

6 files changed

+459
-125
lines changed

6 files changed

+459
-125
lines changed

exercises/concept/recycling-robot/.docs/hints.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -40,14 +40,14 @@
4040
- `typeof` returns a string.
4141
- You can check the length of an array to find out how many elements it contains.
4242

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

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

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

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

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

exercises/concept/recycling-robot/.docs/instructions.md

Lines changed: 31 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,8 @@ isObject(25n);
5252

5353
### 4. Check if a string is numeric
5454

55-
Implement the `isNumericString` function, that should check if the value is a string that only consists of digits.
55+
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.
56+
Only integers should be considered, decimals are not considered numeric for this check of the recycling robot.
5657

5758
```javascript
5859
isNumericString(42);
@@ -109,21 +110,7 @@ isEmptyArray([]);
109110
// => true
110111
```
111112

112-
### 8. Throw an error if an object does not have an `id` property or method
113-
114-
Implement the `assertHasId` function, that will throw an `Error` if an object is missing the `id` property.
115-
116-
If an object does have the `id` property, it should not return anything.
117-
118-
```javascript
119-
assertHasId({ id: 42, color: 'red' });
120-
// => undefined
121-
122-
assertHasId({ color: 'green' });
123-
// Error: "Object is missing the 'id' property"
124-
```
125-
126-
### 9. Check if an object has a `type` property or method
113+
### 8. Check if an object has a `type` property or method
127114

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

@@ -133,40 +120,62 @@ class Keyboard(){
133120
// ...
134121
}
135122
}
136-
hasType({type:"car",color:"red"})
123+
hasType({ type:"car", color:"red" })
137124
// => true
138125

139-
hasType({color:"green"})
126+
hasType({ color:"green" })
140127
// => false
141128

142129
hasType(new Keyboard())
143130
// => true
144131
```
145132

133+
### 9. Throw an error if an object does not have an `id` property or method
134+
135+
Implement the `assertHasId` function, that will throw an `Error` if an object is missing the `id` property.
136+
137+
If an object does have the `id` property, it should not return anything.
138+
139+
```javascript
140+
assertHasId({ id: 42, color: 'red' });
141+
// => undefined
142+
143+
assertHasId({ color: 'green' });
144+
// Error: "Object is missing the 'id' property"
145+
```
146+
146147
### 10. Check if an object has an `id` property
147148

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

150151
```javascript
151-
class MyClass {
152+
class SimpleData {
152153
constructor() {
153154
this.number = '42';
154155
this.id = 'BC269327FE1D9B95';
155156
}
156157
}
157-
class MyNewClass {
158+
159+
class StealingData extends SimpleData {}
160+
161+
class MethodData {
158162
constructor() {
159163
this.number = '42';
160164
this._id = 'BC269327FE1D9B95';
161165
}
166+
162167
get id() {
163168
return this._id;
164169
}
165170
}
166-
hasIdProperty(new MyClass());
171+
172+
hasIdProperty(new SimpleData());
167173
// => true
168174

169-
hasIdProperty(new MyNewClass());
175+
hasIdProperty(new MethodData());
176+
// => false
177+
178+
hasIdProperty(new StealingData());
170179
// => false
171180
```
172181

exercises/concept/recycling-robot/.meta/exemplar.js

Lines changed: 14 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -24,9 +24,7 @@ export function isBoolean(value) {
2424
*/
2525
export function isNumber(value) {
2626
return (
27-
(typeof value === 'number' || typeof value === 'bigint') &&
28-
!isNaN(Number(value)) &&
29-
value !== Infinity
27+
(typeof value === 'number' && isFinite(value)) || typeof value === 'bigint'
3028
);
3129
}
3230

@@ -47,12 +45,7 @@ export function isObject(value) {
4745
* @returns {boolean} whether the input is a numeric string.
4846
*/
4947
export function isNumericString(value) {
50-
return (
51-
typeof value === 'string' &&
52-
value.split('').every((char) => {
53-
return /[0-9]/.test(char);
54-
})
55-
);
48+
return typeof value === 'string' && /^-?\d+$/.test(value);
5649
}
5750

5851
/**
@@ -86,26 +79,27 @@ export function isEmptyArray(value) {
8679
}
8780

8881
/**
89-
* Throws an error if an object is missing an "id" property or method.
82+
* Checks if a value has a "type" property or method.
9083
*
9184
* @param {object} object
92-
* @returns {boolean} undefined if the input has an "id" property, otherwise throws an error.
85+
* @returns {boolean} whether the input has a "type" property.
9386
*/
94-
export function assertHasId(object) {
95-
if ('id' in object) {
96-
return;
97-
}
98-
throw new Error('The "id" property is missing.');
87+
export function hasType(object) {
88+
return 'type' in object;
9989
}
10090

10191
/**
102-
* Checks if a value has a "type" property or method.
92+
* Throws an error if an object is missing an "id" property or method.
10393
*
10494
* @param {object} object
105-
* @returns {boolean} whether the input has a "type" property.
95+
* @returns {never|void} undefined if the input has an "id" property, otherwise throws an error.
10696
*/
107-
export function hasType(object) {
108-
return 'type' in object;
97+
export function assertHasId(object) {
98+
if ('id' in object) {
99+
return;
100+
}
101+
102+
throw new Error('The "id" property is missing.');
109103
}
110104

111105
/**

exercises/concept/recycling-robot/assembly-line.js

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -81,23 +81,23 @@ export function isEmptyArray(value) {
8181
}
8282

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

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

103103
/**

0 commit comments

Comments
 (0)