Java script help

Java script help
0

function Bin2Dec(bin_text_box, output_div) {
var binstr = document.getElementById(bin_text_box).value;

// if we remove all 0's and 1's, is there anything left?
var bad_digits = binstr.replace(/0/g, '').replace(/1/g, '').length;;
var response = '';
var position = 32768;
var num = 0;
var strlen;

if (bad_digits > 0) {
	response = 'Error: You entered an invalid binary number';
}
else {
	if (binstr.length > 16) {
		response = 'Error: Binary number is too long';
	}
	else {
		// We have a valid binary number, let's convert
		
		// Start by adding leading 0's
		binstr = '0000000000000000' + binstr;
		
		// Grab rightmost 16 characters
		binstr = binstr.substring(binstr.length - 16);
		
		strlen = binstr.length;
		
		while (position > 0) {
			if (position.value = 1) {		// If left-most digit is a 1
				num = 'position value;' + num;	// Add position value to num
			}
			
			strlen--;
			binstr = binstr - 0 ;	// Drop left-most digit from binstr
			position = Math.floor(position);	// Next position
		}
		
		response = 'Decimal value: ' + num;
	}
}
	
document.getElementById(output_div).innerHTML = response;

}
// comments tell me what to do but i dont think i have done them right

@realfunk Welcome to the Free Code Camp forum.

Can you explain what you are expecting your code to do vs. what it does do?

Give an example of input and expected output.

FYI - I see some issues even without understanding exactly what you expecting your code to do.

if ((position.value = 1)) {

position is a number and numbers to not have a value property in JavaScript and you can not add one. Also, do you understand what = attempts to do here? It is not what you think.

My professor has tasked us with creating a binary to decimal converter given this format with 16 leading zero’s. For example we could input 10101 (binary) and get the decimal version of this number.

Then I suggest you start fixing the issues I have pointed out above and keep trying.

FYI - There is a JavaScript function you can use called parseInt that would give you the correct conversion without all of the following:

		// Start by adding leading 0's
		binstr = '0000000000000000' + binstr;
		
		// Grab rightmost 16 characters
		binstr = binstr.substring(binstr.length - 16);
		
		strlen = binstr.length;
		
		while (position > 0) {
			if (position.value = 1) {		// If left-most digit is a 1
				num = 'position value;' + num;	// Add position value to num
			}
			
			strlen--;
			binstr = binstr - 0 ;	// Drop left-most digit from binstr
			position = Math.floor(position);	// Next position
		}