Question
Tell me what is wrong with my code for this Web Scripting Technology assignment. I cannot convert Binary to Decimal everything else works as instructed
Tell me what is wrong with my code for this Web Scripting Technology assignment. I cannot convert Binary to Decimal everything else works as instructed
Binary-Decimal Conversion (50 pts + 5 ex.cr.) 2023
For this assignment you will develop a web page which can convert a binary number to decimal, or a decimal number to binary. You are to accomplish this in javascript by coding the specific algorithms shown below for each of these processes. A starter screen has been provided, as follows:
Part A (Decimal to Binary):
An algorithm for converting a decimal value into its binary equivalent involves repeated (integer) division by 2 (down to a result of 0), collection of the remainders, and reading those remainders backwards. (A sample run is shown below).
One approach to this algorithm (that will be shown via lecture) is to accumulate the remainders in an array, and then step through the array backwards to show the results in proper order.
A more elegant approach is to use a recursive algorithm to calculate the remainder, which will allow direct output of the results and which does not have any practical limitations on the size of the number being converted. This approach is an extra credit option for the assignment.
The validation of the decimal value means checking that it is a positive integer.
A sample run for Decimal to Binary:
Note on Integer Division:
As you can see from the example above, this conversion process requires integer division and the modulus operation. The modulus (or remainder) operator is the % sign. Integer division, however, poses a slight problem for javascript, since all numeric variables are intrinsically real numbers. One technique around this problem is to insure that the result of each division by 2 will be an integer before the division is performed. This particular trick can be accomplished by subtracting the expected remainder before the division. For example, If we need to convert the number 5 to binary, we would do the following:
5 % 2 = 1
5 / 2 = 2 (integer result)
etc.
But javascript would produce a result of 2.5 for the 5/2 part, instead of an integer. If, however, we subtract the remainder from the original number before the division by two, we get:
5 % 2 = 1
(5 1) / 2 = 2 (integer result)
Part B (Binary to Decimal):
A standard algorithm for a binary to decimal conversion evaluates the positional values of the binary digits. For example, the binary value of: 1011 is 11 because you have the following:
Binary powers of 2: 2^3 2^2 2^1 2^0
Positional value: 8 4 2 1
Binary digits: 1 0 1 1
So the value = (1*8) + (0*4) + (1*2) + (1*1) = 11
Likewise, the binary value of 11111101 = 253 because you have the following:
Powers of 2: 2^7 2^6 2^5 2^4 2^3 2^2 2^1 2^0
Positional values: 128 64 32 16 8 4 2 1
Binary digits: 1 1 1 1 1 1 0 1
So the value = 128 + 64 + 32 + 16 + 8 + 4 + 1 = 253
In coding the To Decimal process remember that you must validate the input as containing only ones and zeros. A sample run might appear as follows:
Call the project BinDecConv, and code it with all javascript code in a separate script file called BinDecConv.js.
Extra Credit (5 pts):
As indicated above, the Part A lecture will use an array to accumulate the remainders. For extra credit add a new radio button for To Binary via Recursion and code the process for that button using a recursive function call (i.e., a function that calls itself, until a termination condition is reached).
/* global convert */
var $ = function(id) { return document.getElementById(id); };
window.onload = function() { $("btnConvert").onclick = convert; $("btnClear").onclick = fnClear; $("txtIn").focus(); }; const isBinary = (value) => { for(let i = 0; i < value.length; i++){ if(value[i] !== "0" && value[i] !== "1") return false; } return true; }; const toDecimal = (value) => { let decimalNumber = 0; let valueLen = value.length; $("txtCalc").value === ""; for(let i = valueLen - 1; i >= 0; i++) { if(value[i] ==="1") { let exp = valueLen -1 -i; let tmpValue = Math.pow( 2,exp); decimalNumber += tmpValue; $("txtCalc").value = $("txtCalc").value + "There is a " + tmpValue + " in \ the value 2^" + exp + ". "; } } let deciNumText = toString(decimalNumber); $("txtOut").value = deciNumText; return decimalNumber; };
function fnClear() { $("Conv").reset(); $("txtCalc").innerHTML = ""; $("txtIn").focus(); }//clear button works function convert() { var val = $("txtIn").value; if (val === "") { alert("no value entered for conversion"); return ; } var cT = document.getElementsByName("tType"); if (cT [0].checked) { //to Binary var dval = parseInt(val); if (isNaN(dval)) { alert("Input value is not a number"); } else if ((val % 1) !==0) { alert("Number is not an integer"); } else if (dval <= 0) { alert("Input value must be a positive integer"); } else { convertByArray(dval); } } else if (cT[1].checked) { //to decimal if (!isBinary(val)) alert("Input value may contain caracter(s) other than 0 & 1"); else convertToBinary(val); } else { alert("Please select a conversion type."); } }//end of convert function convertByArray(dval) { var rA = new Array(); var r,i,j; i=0; while (dval > 0) { r = dval % 2; //remainder rA[i] = r; var nV = (dval - r) / 2; $("txtCalc").value = $("txtCalc").value + "Decimal " + dval + " divided by 2 = " + nV + " w/Remainder of: " + r + " "; i += 1; dval = nV; } for(j=rA.length-1; j>= 0; j--) { $("txtOut").value = $("txtOut").value + rA[j]; }
}
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started