You have been hired by a recycling center. Due to lack of space, all the products are put on the same conveyor belt, but this has lead to different materials mixing together, making them unusable. To fix this, you have been tasked with making functions to identify the type of a product.
Implement the isBoolean function, that checks if a value is a boolean.
isBoolean(true);
// => true
isBoolean(null);
// => falseImplement the isNumber function, that checks if a value is a finite number or bigint, ie. not NaN or Infinity.
Sometimes, the device for reading IDs fails and reads a non-numeric value as NaN (Not a Number) or Infinity.
Your function should be able to correctly handle this as well.
isNumber(42);
// => true
isNumber('Hello, World!');
// => false
isNumber(42n);
// => true
isNumber(NaN);
// => falseImplement the isObject function, that should check if the value is an object.
On the conveyor, null is nothing and not considered an object.
isObject({ greeting: 'Hello' });
// => true
isObject(25n);
// => falseImplement 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.
isNumericString(42);
// => false
isNumericString('42');
// => true
isNumericString('Hi!');
// => falseImplement the isElectronic function, that checks if an object is an instance of the provided ElectronicDevice class or one of its child classes.
class Duck {
//...
}
class WashingMachine extends ElectronicDevice {
//...
}
isElectronic(new Duck());
// => false
isElectronic(new WashingMachine());
// => trueImplement the isNonEmptyArray function, that checks if a value is a non-empty array.
isNonEmptyArray([1, 2, 3]);
// => true
isNonEmptyArray([]);
// => falseImplement the isEmptyArray function, that checks if a value is an empty array.
isEmptyArray([1, 2, 3]);
// => false
isEmptyArray([]);
// => truetype property or methodImplement the hasType function, that checks whether an object has a type property or method.
class Keyboard(){
type(){
// ...
}
}
hasType({ type:"car", color:"red" })
// => true
hasType({ color:"green" })
// => false
hasType(new Keyboard())
// => trueid property or methodImplement 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.
assertHasId({ id: 42, color: 'red' });
// => undefined
assertHasId({ color: 'green' });
// Error: "Object is missing the 'id' property"id propertyImplement the hasIdProperty function, that checks whether an object has an id property.
class SimpleData {
constructor() {
this.number = '42';
this.id = 'BC269327FE1D9B95';
}
}
class StealingData extends SimpleData {}
class MethodData {
constructor() {
this.number = '42';
this._id = 'BC269327FE1D9B95';
}
get id() {
return this._id;
}
}
hasIdProperty(new SimpleData());
// => true
hasIdProperty(new MethodData());
// => false
hasIdProperty(new StealingData());
// => falsetype propertyImplement the hasDefinedType function, that checks if an object has a type property that is not undefined.
hasDefinedType({ type: undefined, color: 'red' });
// => false
hasDefinedType({ type: 'car', color: 'green' });
// => trueYou have been hired by a recycling center. Due to lack of space, all the products are put on the same conveyor belt, but this has lead to different materials mixing together, making them unusable. To fix this, you have been tasked with making functions to identify the type of a product.
Implement the isBoolean function, that checks if a value is a boolean.
isBoolean(true);
// => true
isBoolean(null);
// => falseImplement the isNumber function, that checks if a value is a finite number or bigint, ie. not NaN or Infinity.
Sometimes, the device for reading IDs fails and reads a non-numeric value as NaN (Not a Number) or Infinity.
Your function should be able to correctly handle this as well.
isNumber(42);
// => true
isNumber('Hello, World!');
// => false
isNumber(42n);
// => true
isNumber(NaN);
// => falseImplement the isObject function, that should check if the value is an object.
On the conveyor, null is nothing and not considered an object.
isObject({ greeting: 'Hello' });
// => true
isObject(25n);
// => falseImplement 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.
isNumericString(42);
// => false
isNumericString('42');
// => true
isNumericString('Hi!');
// => falseImplement the isElectronic function, that checks if an object is an instance of the provided ElectronicDevice class or one of its child classes.
class Duck {
//...
}
class WashingMachine extends ElectronicDevice {
//...
}
isElectronic(new Duck());
// => false
isElectronic(new WashingMachine());
// => trueImplement the isNonEmptyArray function, that checks if a value is a non-empty array.
isNonEmptyArray([1, 2, 3]);
// => true
isNonEmptyArray([]);
// => falseImplement the isEmptyArray function, that checks if a value is an empty array.
isEmptyArray([1, 2, 3]);
// => false
isEmptyArray([]);
// => truetype property or methodImplement the hasType function, that checks whether an object has a type property or method.
class Keyboard(){
type(){
// ...
}
}
hasType({ type:"car", color:"red" })
// => true
hasType({ color:"green" })
// => false
hasType(new Keyboard())
// => trueid property or methodImplement 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.
assertHasId({ id: 42, color: 'red' });
// => undefined
assertHasId({ color: 'green' });
// Error: "Object is missing the 'id' property"id propertyImplement the hasIdProperty function, that checks whether an object has an id property.
class SimpleData {
constructor() {
this.number = '42';
this.id = 'BC269327FE1D9B95';
}
}
class StealingData extends SimpleData {}
class MethodData {
constructor() {
this.number = '42';
this._id = 'BC269327FE1D9B95';
}
get id() {
return this._id;
}
}
hasIdProperty(new SimpleData());
// => true
hasIdProperty(new MethodData());
// => false
hasIdProperty(new StealingData());
// => falsetype propertyImplement the hasDefinedType function, that checks if an object has a type property that is not undefined.
hasDefinedType({ type: undefined, color: 'red' });
// => false
hasDefinedType({ type: 'car', color: 'green' });
// => true