5 kyu

Calculating with objects

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

Stats:

CreatedJul 14, 2018
PublishedJul 14, 2018
Warriors Trained660
Total Skips19
Total Code Submissions1024
Total Times Completed118
JavaScript Completions118
Total Stars44
% of votes with a positive feedback rating88% of 32
Total "Very Satisfied" Votes26
Total "Somewhat Satisfied" Votes4
Total "Not Satisfied" Votes2
Total Rank Assessments5
Average Assessed Rank
6 kyu
Highest Assessed Rank
4 kyu
Lowest Assessed Rank
8 kyu
Ad
Contributors
  • Torkel Avatar
  • e.mihaylin Avatar
Ad