problems(js): add “2695. Array Wrapper”
Signed-off-by: Matej Focko <mfocko@redhat.com>
This commit is contained in:
parent
de8ef50ac8
commit
817d40aa95
1 changed files with 30 additions and 0 deletions
30
problems/js/array-wrapper.js
Normal file
30
problems/js/array-wrapper.js
Normal file
|
@ -0,0 +1,30 @@
|
||||||
|
/**
|
||||||
|
* @param {number[]} nums
|
||||||
|
*/
|
||||||
|
var ArrayWrapper = function(nums) {
|
||||||
|
this.nums = nums;
|
||||||
|
|
||||||
|
return this;
|
||||||
|
};
|
||||||
|
|
||||||
|
ArrayWrapper.prototype.valueOf = function() {
|
||||||
|
let sum = 0;
|
||||||
|
|
||||||
|
for (let x of this.nums) {
|
||||||
|
sum += x;
|
||||||
|
}
|
||||||
|
|
||||||
|
return sum;
|
||||||
|
}
|
||||||
|
|
||||||
|
ArrayWrapper.prototype.toString = function() {
|
||||||
|
return `[${this.nums.join(',')}]`;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* const obj1 = new ArrayWrapper([1,2]);
|
||||||
|
* const obj2 = new ArrayWrapper([3,4]);
|
||||||
|
* obj1 + obj2; // 10
|
||||||
|
* String(obj1); // "[1,2]"
|
||||||
|
* String(obj2); // "[3,4]"
|
||||||
|
*/
|
Loading…
Reference in a new issue