5 kyu
Calculating with objects
118Torkel
Description:
Bob made a class called Num.
class Num {
constructor(num){
this.num=num;
}
toString() {
return "The number is " + this.num;
}
valueOf() {
return {num: this.num};
}
}
He's trying to work with it like this:
x = new Num(100);
y = new Num(5);
x + y == 105;
x*y == 500;
x-y == 95
x/y == 20
Math.floor(new Num(100.5)) == 100
etc..
But it doesn't work.. He keeps getting '[object Object][object Object]'
as a result..
He also wants to be able to use the existing valueOf
and toString
methods, so we shouldn't change those.
x = new Num(100);
x.toString() == "The number is " + num
x.valueOf() == {num: this.num}
Can you help him make his class work?
The class is preloaded.
Puzzles
Similar Kata:
Stats:
Created | Jul 14, 2018 |
Published | Jul 14, 2018 |
Warriors Trained | 660 |
Total Skips | 19 |
Total Code Submissions | 1024 |
Total Times Completed | 118 |
JavaScript Completions | 118 |
Total Stars | 44 |
% of votes with a positive feedback rating | 88% of 32 |
Total "Very Satisfied" Votes | 26 |
Total "Somewhat Satisfied" Votes | 4 |
Total "Not Satisfied" Votes | 2 |
Total Rank Assessments | 5 |
Average Assessed Rank | 6 kyu |
Highest Assessed Rank | 4 kyu |
Lowest Assessed Rank | 8 kyu |