6 kyu

Hex class

25 of 1,572josher19

Description:

Create a class Hex which takes a number as an argument.

Adding a hex object to a number (by using valueOf) generates a number, but calling toString or toJSON will show its hexadecimal value starting with "0x". To make hex objects comparable you have to provide a method equals.

Example:

var FF = new Hex(255);
FF.toString() == "0xFF";
FF.valueOf() + 1 == 256;
FF = new Hex(255)
FF.toString() == "0xFF"
FF.valueOf() + 1 == 256
Hex FF = new Hex(255);
FF.toString() == "0xFF"
FF.valueOf() + 1 == 256
FF.equals(new Hex(255)) == true

Also create two methods, plus and minus which will add or subtract a number or Hex object and return a new Hex object.

var a = new Hex(10);
var b = new Hex(5);
a.plus(b).toJSON() == "0xF";
a = new Hex(10)
b = new Hex(5)
a.plus(b).toJSON() == "0xF"
Hex a = new Hex(10);
Hex b = new Hex(5);
a.plus(b).toJSON() == "0xF";
a.plus(2).toJSON() == "0xC";

Also, create a parse class method that can parse Hexadecimal numbers and convert them to standard decimal numbers:

Hex.parse("0xFF") == 255;
Hex.parse("FF") == 255;
Hex.parse("0xFF") == 255
Hex.parse("FF") == 255
Hex.parse("0xFF") == 255
Hex.parse("FF") == 255

Note: If you define both valueOf and toString, "Hex value:" + new Hex(255) may not behave as expected!

Algorithms
Parsing
Fundamentals
Object-oriented Programming

Stats:

CreatedDec 6, 2014
PublishedDec 6, 2014
Warriors Trained3030
Total Skips357
Total Code Submissions9957
Total Times Completed1572
CoffeeScript Completions25
JavaScript Completions1296
Java Completions267
Total Stars56
% of votes with a positive feedback rating91% of 175
Total "Very Satisfied" Votes146
Total "Somewhat Satisfied" Votes26
Total "Not Satisfied" Votes3
Total Rank Assessments24
Average Assessed Rank
6 kyu
Highest Assessed Rank
3 kyu
Lowest Assessed Rank
7 kyu
Ad
Contributors
  • josher19 Avatar
  • jhoffner Avatar
  • ZozoFouchtra Avatar
  • smile67 Avatar
  • ParanoidUser Avatar
  • _mer_ Avatar
Ad