Binary Gap In Javascript.

What is Binary Gap.

The binary gap of a number is defined as the maximum number of consecutive zeros that occur between two ones in the binary representation of the number.

Program

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<html>
<title>Binary Gap</title>
<body>
<center>
<label>Enter Number:</label><input type="number" id="number">
<button type="button" id="btn" onclick="findGap()">Click to Check Binary Gap!</button><br>
<hr>
<h2 style="color:green" id="binary"></h2>
<h2 style="color:green" id="binarygap"></h2>
</center>
</body>
<script>
function findGap(){
let number = parseInt(document.getElementById('number').value);
const binaryNumber = number.toString(2);
var binaryGap = [], gapLength = 0 ,found = false;
document.getElementById('binary').innerHTML = 'Binary Number : '+binaryNumber;
for (let index = 0; index < binaryNumber.length; index++) {
if(binaryNumber[index] == 1){
for(let j = index+1; j<=binaryNumber.length; j++){
if(binaryNumber[j]==0){gapLength++;}
if(binaryNumber[j]==1) { found = true;break;}
}
if(found){
binaryGap.push(gapLength);
found = false;
}
}
gapLength=0;
}
if(binaryGap.length>0){
document.getElementById('binarygap').innerHTML = 'Largest Binary Gap is : '+ Math.max.apply(Math,binaryGap);
}else{
document.getElementById('binarygap').innerHTML = 'Largest Binary Gap is : '+ 0;
}
}
</script>
</html>
<html> <title>Binary Gap</title> <body> <center> <label>Enter Number:</label><input type="number" id="number"> <button type="button" id="btn" onclick="findGap()">Click to Check Binary Gap!</button><br> <hr> <h2 style="color:green" id="binary"></h2> <h2 style="color:green" id="binarygap"></h2> </center> </body> <script> function findGap(){ let number = parseInt(document.getElementById('number').value); const binaryNumber = number.toString(2); var binaryGap = [], gapLength = 0 ,found = false; document.getElementById('binary').innerHTML = 'Binary Number : '+binaryNumber; for (let index = 0; index < binaryNumber.length; index++) { if(binaryNumber[index] == 1){ for(let j = index+1; j<=binaryNumber.length; j++){ if(binaryNumber[j]==0){gapLength++;} if(binaryNumber[j]==1) { found = true;break;} } if(found){ binaryGap.push(gapLength); found = false; } } gapLength=0; } if(binaryGap.length>0){ document.getElementById('binarygap').innerHTML = 'Largest Binary Gap is : '+ Math.max.apply(Math,binaryGap); }else{ document.getElementById('binarygap').innerHTML = 'Largest Binary Gap is : '+ 0; } } </script> </html>
<html>    
    <title>Binary Gap</title>
    <body>
        <center>
            <label>Enter Number:</label><input type="number" id="number">
            <button type="button" id="btn" onclick="findGap()">Click to Check Binary Gap!</button><br>
            <hr>
            <h2 style="color:green" id="binary"></h2>
            <h2 style="color:green" id="binarygap"></h2>
        </center>
    </body>
    <script>       
        function findGap(){            
            let number = parseInt(document.getElementById('number').value);    
            const binaryNumber = number.toString(2);         
            var binaryGap = [], gapLength = 0 ,found = false;            
            document.getElementById('binary').innerHTML = 'Binary Number : '+binaryNumber;
            for (let index = 0; index < binaryNumber.length; index++) {                 
                if(binaryNumber[index] == 1){                     
                    for(let j = index+1; j<=binaryNumber.length; j++){
                        if(binaryNumber[j]==0){gapLength++;}
                        if(binaryNumber[j]==1) { found = true;break;}
                    }
                    if(found){
                        binaryGap.push(gapLength);
                        found = false;
                    }
                }
                gapLength=0;
            }   
            if(binaryGap.length>0){
                document.getElementById('binarygap').innerHTML = 'Largest Binary Gap is : '+ Math.max.apply(Math,binaryGap);
            }else{
                document.getElementById('binarygap').innerHTML = 'Largest Binary Gap is : '+ 0;
            }        
        }
        </script>
</html>

 

Back to top button