Array Wrapper - Problem
Create a class ArrayWrapper that accepts an array of integers in its constructor. This class should have two features:
Addition Operator: 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.
String Conversion: When the String() function is called on the instance, it will return a comma separated string surrounded by brackets. For example, [1,2,3].
Input & Output
Example 1 — Basic Addition and String
$
Input:
nums1 = [1,2], nums2 = [3,4]
›
Output:
[10, "[1,2]"]
💡 Note:
obj1 + obj2 = (1+2) + (3+4) = 3 + 7 = 10. String(obj1) = "[1,2]"
Example 2 — Empty Array
$
Input:
nums1 = [], nums2 = [5]
›
Output:
[5, "[]"]
💡 Note:
obj1 + obj2 = 0 + 5 = 5. String(obj1) = "[]" for empty array
Example 3 — Negative Numbers
$
Input:
nums1 = [-1,-2], nums2 = [1]
›
Output:
[-2, "[-1,-2]"]
💡 Note:
obj1 + obj2 = (-1+-2) + 1 = -3 + 1 = -2. String includes negative numbers
Constraints
- 1 ≤ nums.length ≤ 1000
- -1000 ≤ nums[i] ≤ 1000
Visualization
Tap to expand
Understanding the Visualization
1
Input
Two arrays [1,2] and [3,4] wrapped in ArrayWrapper objects
2
Process
Addition operator sums all elements, String() formats with brackets
3
Output
Returns [10, "[1,2]"] - sum and string representation
Key Takeaway
🎯 Key Insight: JavaScript's valueOf() and toString() methods enable seamless operator overloading
💡
Explanation
AI Ready
💡 Suggestion
Tab
to accept
Esc
to dismiss
// Output will appear here after running code