Enter Value to Convert:
Direction: To Binary To Decimal
Calculations:
The Value is:
need help on finishing the code. INSTRUCTIONS:
HTML:
Enter Value to Convert:
Direction: To Binary To Decimal
Calculations:
The Value is:
CSS: /* CSS Document */ body { background-color: lightcyan; } #content { width: 600px; margin: 20px; text-align: center; } #txtDec { width: 150px; text-align: right; } #txtBin { width: 400px; text-align: center; } #txtCalc { height: auto; } .left { text-align: left; } JS code:
var $ = function(id) { return document.getElementById(id); };
window.onload = function() { $("btnConvert").onclick = convert; $("txtIn").focus(); };
function convert() { var val = $("txtIn").value; if (val === ""){ alert("no value entered for conversion"); return; } var cT = document.getElementByName("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("Input value must be a positive integer"); } else { convertByArray(dval); } } else if (cT[1].checked) { //to decimal } 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; 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]; } } HUGE NOTE: HTML AND CSS ARE NOT ABLE TO BE EDITTED
IS265: Scripting Technologies 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 algorithims shown below for each of these processes. A starter screen has been provided, as follows: Enter Value to Convert: Direction: To Binary To Decimal Calculations: The Value is: Part 4 (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). 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. Direction: To Binary To Decimal Calculations: 6 divided by 2=3w/ remainder of: 0 3 divided by 2=1w/remainder of 1 1 divided by z=0w/remainder of: 1 The Value is: 1101 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: 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(51)/2=2(integerresult) 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: 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: So the value =(18)+(04)+(12)+(11)=11 Likewise, the binary value of 11111101=253 because you have the following: 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: 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: [ D. Decimal / Binary Converter localhoat 8383/BinDecConv/BinDecConv/html FT=c Enter Value to Convert: Direction: To Binary To Decimal CalralatinneThere are 3 Steps involved in it
See step-by-step solutions with expert insights and AI powered tools for academic success
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started