Create a program to compute the sum of two binary numbers or two hexadecimal numbers, both given in string format.




You are working on an application that operates with strings containing binary integers or hexadecimal integers (without any prefixes like 0b or 0x).

One particular task in the application requires the ability to add two binary or hexadecimal strings to one another and then return the output in the same format.

In this exercise, you have to create a program that abides by the following description.

Create two modules, each with a public add() function; one to work with binary strings and one to work with hexadecimal strings. Note that both the functions must be called add().

Then create a main module, referring to both of these modules, that prompts the user to select which addition to perform via a browser prompt dialogue and then performs the corresponding addition on two input numbers.

The initial input prompt meant for obtaining the type of addition desired works as follows:

  • 'bin' means a binary string addition while 'hex' means an addition in the hexadecimal format.
  • Any other input should lead to an alert message, saying 'Invalid addition type!', before finally ending the program.

Following this initial prompt, supposing that the correct input is given, the program should ask for two further inputs that represent the numbers to be added together.

After each input, if the entered value is not valid for the underlying number format, make the alert 'Invalid number entered!' and terminate the program.

In the end, once everything works fine, the given numbers should be added together, and the result output in the same format in which the numbers were given.

Here's a live example for you to better understand the program:

Live Example