0

I want to pass array from function to another in java script, but when I make it, the browser stalk, I don't know why . here's my code:

 function convertToBinary(decNumber){

            var copyDecNum=Number(decNumber);
            var binaryValues= new Array();
            var cnt=0;
               while(copyDecNum.value!=0)
            {
                binaryValues[cnt]=Math.floor(copyDecNum.value%2);
                copyDecNum.value=Math.floor(copyDecNum.value/2);
                cnt++;

            }
            binaryValues[cnt]=copyDecNum%2;
            viewResult(binaryValues,decNumber);



        }

        function viewResult(binaryValues,decNumber){

          alert("here"+binaryValues.length);           //here's the problem
          }

can someone help??

2
  • 1
    What is this line for binaryValues[cnt]=copyDecNum%2;? I understand, that your trying to convert decimal numbers into binary. And the algorithm seems right but what do expect from this line? If you want to append it as last loop shouldn't it be binaryValues[cnt]=copyDecNum.value%2; ? Commented Mar 16, 2011 at 7:47
  • yes u r right, it dones't have any meaning. Commented Mar 16, 2011 at 7:59

2 Answers 2

3

If you want to convert a decimal number to binary use the following,

var dec2bin = function (num) {
  return +(num.toString(2)) //convert number to binary string, then make that a number
}
Sign up to request clarification or add additional context in comments.

1 Comment

there's nothing strange in this. I've just utilized what the language provides. Ref: MDC
0

Use copyDecNum instead of copyDecNum.value in your code.

1 Comment

I need the value, so I have to use .value

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.