mplement a class named ArrayWrapper whose constructor accepts an array of integers as a parameter. The class should have the following features:
When using the + operator to add two instances of this class, the result should be the sum of all elements in the two arrays.
When calling the String() function on an instance, it should return a comma-separated string of the array elements, enclosed in square brackets. For example, [1,2,3].
// Example 1:
Input: nums = [[1,2],[3,4]], operation = "Add"
Output: 10
Explanation:
const obj1 = new ArrayWrapper([1,2]);
const obj2 = new ArrayWrapper([3,4]);
obj1 + obj2; // 10
// Example 2:
Input: nums = [[23,98,42,70]], operation = "String"
Output: "[23,98,42,70]"
Explanation:
const obj = new ArrayWrapper([23,98,42,70]);
String(obj); // "[23,98,42,70]"
class ArrayWrapper {
constructor(nums){
this.nums = nums
}
valueOf(){
return this.nums.reduce((sum, value) => sum + value, 0);
}
toString(){
return `[${this.nums.join(',')}]`;
}
}
function ArrayWrapper(nums) {
this.nums = nums;
};
ArrayWrapper.prototype.valueOf = function() {
return this.nums.reduce((sum, value) => sum + value, 0);
}
ArrayWrapper.prototype.toString = function() {
return `[${this.nums.join(',')}]`;
}