Program 3: Binary to decimal converter
Due: Thursday
Oct 11, 11:59pm EST
Motivation
Converting between binary and decimal is tedious by hand. Create a
JavaScript program to convert a binary number into the corresponding
decimal number. The binary value could be any number of bits.
User interface
Create an HTML form similar to program 1 and program 2. Include these
form elements in the give order:
- An input field to enter the binary number
- A button that makes the calculates the conversion
- An input field that displays the number
- Add instructions to the web page that explain how the converter
should be used.
Using the interface
- User enters a binary value
- User presses the button
- The program displays the result in the third field
Output
There are two parts to the output.
First, your program validates that the input is a valid binary value.
The binary value is valid if it consists of "1" and "0" characters. If
the binary value is invalid, the result should be:
Invalid
binary number
Second, if the binary value is valid then your program returns the
decimal value. The binary value can be any number of bits.
Objective
The objective of program 3 is to design a program using a loop and
string (array of characters). It also gives practice with converting
binary/decimal numbers.
The main work in this program (ie. most of the time spent) is to design
the algorithm (procedure) to convert the binary to decimal number. It
requires looping through the characters of a string and computing the
decimal value.
Here is an outline of the program (validate input and convert binary
value)
- Read the binary value
- Check if the binary number is valid.
- If the binary value is valid, convert the binary value into the
decimal value.
- Display the result. Either, invalid input or the decimal value.
I suggest separating the process of validating and converting the
binary value.
Tips
- See part 3 of the array and loops recitation (recitation, part 3) for an example
looping through a string.
- Practice converting binary values to decimal values on paper.
Determine the procedure to loop through the string and compute the
conversion.
- Any loops you have are short. The final loop will be of similar
complexity of question 12 in homework 3. The code in question 12
converted a decimal value into a binary number. Here
is the same
program as an HTML program
- See the program 3 tester for examples of binary numbers converted
to decimal
- Notice the pattern of the binary values. The bit value doubles
each time, starting at 1. The value of the bits starting with the least
significant bit: 1, 2, 4, 8, 16, 32, 64, 128, etc.
- You may find the Math.pow(base, power) function useful.
Math.pow(base, power) computes the value of the base raised to the
power. This gives the value of the bit. Example: Math.pow(2,3) = 8,
Math.pow(2,4) = 16. More details on
Math.pow().
Testing
Use the following online testing application to test your program. The
tester demonstrates more examples of valid and invalid binary values.
Your program must work with the tester; this is how we will grade your
program.
Grading
We will grade your HTML and JavaScript programming based on the
following.
- 75 point for working program
- -10 points if the program does not work with the tester (see
testing below)
- -10 points for various test cases. 1 or 2
of 7 score, then -8 points. Otherwise -2 for incorrect
answer.
- -3 incorrect punctuation and spaces when computation is
correct.
- -5 Confusing or incorrect computation
- -75 for a non-working program or program that produces no
output
- 10 HTML tags
- -5 incomplete instructions on the web page explaining the
game to players.
- -5 HTML form with button and fields for input and output
- -3 Main HTML tags (html, head, body, script, form)
- -2 Proper use of quotes on tag attributes
- -2 HTML filename that is not secret enough. That is if it has
the words program, banner, complete words, etc
- 15 JavaScript program
- -5 Reasonable variable names appropriate for the application
- -5 Comments and Indentation
- -5 Use of constants; every number must be a constant
Programming and Submission
Store your HTML file on your local disk until it's finished, then save
it into your Onyen public_html directory using a secret 8-character
name. Do not link your calculator page to other pages in that directory.
When your assignment is ready, you will submit your assignment through
Blackboard.