Overview
Implement ArrayWrapper class:
valueOf()returns the sum of elementstoString()returns the elements as[1,2,3]string- Adding two wrappers sums their internal arrays
Examples
const a = new ArrayWrapper([1,2]); const b = new ArrayWrapper([3,4]); a + b; // 10 String(a); // '[1,2]'
Solution
Reveal solution
class ArrayWrapper {
constructor(nums) { this.nums = nums; }
valueOf() { return this.nums.reduce((a, b) => a + b, 0); }
toString() { return '[' + this.nums.join(',') + ']'; }
}array-wrapper.js
Array Wrapper
easycodingJavaScriptClasses
Overview
Implement ArrayWrapper class:
valueOf()returns the sum of elementstoString()returns the elements as[1,2,3]string- Adding two wrappers sums their internal arrays
Examples
const a = new ArrayWrapper([1,2]); const b = new ArrayWrapper([3,4]); a + b; // 10 String(a); // '[1,2]'
Solution
Reveal solution
class ArrayWrapper {
constructor(nums) { this.nums = nums; }
valueOf() { return this.nums.reduce((a, b) => a + b, 0); }
toString() { return '[' + this.nums.join(',') + ']'; }
}NameTopicDifficulty
103 of 103 problems