-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathArray Wrapper.js
More file actions
36 lines (26 loc) · 1017 Bytes
/
Array Wrapper.js
File metadata and controls
36 lines (26 loc) · 1017 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
// Create a class ArrayWrapper that accepts an array of integers in its constructor. This class should have two features:
// When two instances of this class are added together with the + operator, the resulting value is the sum of all the elements in both arrays.
// When the String() function is called on the instance, it will return a comma separated string surrounded by 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
//code
var ArrayWrapper = function(nums) {
this.nums = nums; // Store the array
};
/**
* @return {number}
*/
ArrayWrapper.prototype.valueOf = function() {
return this.nums.reduce((sum, num) => sum + num, 0); // Sum all elements
};
/**
* @return {string}
*/
ArrayWrapper.prototype.toString = function() {
return `[${this.nums.join(",")}]`; // Convert array to string format [1,2,3]
};