palAlaa
palAlaa

Reputation: 9858

pass array from javascript function to another

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??

Upvotes: 0

Views: 741

Answers (2)

Livingston Samuel
Livingston Samuel

Reputation: 2422

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
}

Upvotes: 3

Nishan
Nishan

Reputation: 2871

Use copyDecNum instead of copyDecNum.value in your code.

Upvotes: 0

Related Questions