Array Wrapper - Problem

Create a powerful ArrayWrapper class that transforms ordinary arrays into smart objects with custom behaviors! ๐Ÿš€

Your mission: Build a JavaScript class that accepts an array of integers in its constructor and implements two magical features:

  • Addition Magic: When two ArrayWrapper instances are added with the + operator, return the sum of all elements from both arrays
  • String Transformation: When String() is called on an instance, return a beautifully formatted comma-separated string surrounded by brackets

Example:

const wrapper1 = new ArrayWrapper([1, 2, 3]); // sum = 6
const wrapper2 = new ArrayWrapper([4, 5]);    // sum = 9
console.log(wrapper1 + wrapper2);             // Output: 15
console.log(String(wrapper1));                // Output: "[1,2,3]"

This problem teaches you about JavaScript operator overloading using valueOf() and toString() methods - essential skills for creating elegant object-oriented solutions!

Input & Output

example_1.js โ€” Basic Addition
$ Input: wrapper1 = new ArrayWrapper([1,2,3]); wrapper2 = new ArrayWrapper([4,5]); wrapper1 + wrapper2
โ€บ Output: 15
๐Ÿ’ก Note: The + operator triggers valueOf() on both objects. wrapper1.valueOf() returns 1+2+3=6, wrapper2.valueOf() returns 4+5=9, so the result is 6+9=15.
example_2.js โ€” String Conversion
$ Input: wrapper = new ArrayWrapper([1,2,3]); String(wrapper)
โ€บ Output: "[1,2,3]"
๐Ÿ’ก Note: String() conversion calls the toString() method, which formats the array as a comma-separated string surrounded by brackets.
example_3.js โ€” Empty Array Edge Case
$ Input: wrapper1 = new ArrayWrapper([]); wrapper2 = new ArrayWrapper([1,2]); wrapper1 + wrapper2
โ€บ Output: 3
๐Ÿ’ก Note: Empty array has sum 0, so 0 + (1+2) = 3. The toString() of empty array would be "[]".

Constraints

  • 1 โ‰ค nums.length โ‰ค 1000
  • -1000 โ‰ค nums[i] โ‰ค 1000
  • Must implement valueOf() and toString() methods
  • The solution should work with JavaScript's native + operator and String() function

Visualization

Tap to expand
ArrayWrapper Classconstructor(nums)valueOf() โ†’ sumtoString() โ†’ formatwrapper1[1,2,3]wrapper2[4,5]+15String(wrapper1)"[1,2,3]"valueOf() โ†’ 6valueOf() โ†’ 9toString() calledโœจ JavaScript Magic:โ€ข + operator โ†’ valueOf()โ€ข String() โ†’ toString()โ€ข Automatic type conversionโ€ข Natural operator usage
Understanding the Visualization
1
Object Creation
ArrayWrapper stores the array and defines special methods
2
Addition Operation
+ operator automatically calls valueOf() on both objects
3
String Conversion
String() function automatically calls toString() method
4
Natural Integration
Objects work seamlessly with JavaScript's built-in operators
Key Takeaway
๐ŸŽฏ Key Insight: JavaScript's valueOf() and toString() methods enable seamless operator overloading, making custom objects work naturally with built-in operators like + and String().
Asked in
Meta 15 Google 12 Amazon 8 Microsoft 6
28.0K Views
Medium Frequency
~12 min Avg. Time
850 Likes
Ln 1, Col 1
Smart Actions
๐Ÿ’ก Explanation
AI Ready
๐Ÿ’ก Suggestion Tab to accept Esc to dismiss
// Output will appear here after running code
Code Editor Closed
Click the red button to reopen