Javascript Function Closure Convert Decimal to Binary

Description

Javascript Function Closure Convert Decimal to Binary

function base10ToString(n) {
    var binaryString = "";

    function base10ToStringHelper(n) {
        if (n < 2) {
            binaryString += n;// w w  w .  jav  a 2  s .  c o  m
            return;
        } else {
            base10ToStringHelper(Math.floor(n / 2));
            base10ToStringHelper(n % 2);
        }
    }
    base10ToStringHelper(n);

    return binaryString;
}

console.log(base10ToString(1)); 
console.log(base10ToString(2)); 
console.log(base10ToString(7));
console.log(base10ToString(8)); 



PreviousNext

Related