Run Code
|
API
|
Code Wall
|
Misc
|
Feedback
|
Login
|
Theme
|
Privacy
|
Patreon
BinaryGap, javascript - Find longest sequence of zeros in binary representation of an integer.
// javascript var iTheNumber = 85, iTheZeros = 0; function binaryGap(num, theZeros, binary) { var iGapCurrently = 0, iGapHigher = 0; sBinary = (iTheNumber >>> 0).toString(2); for (var position = 0; position < sBinary.length; position++) { if (sBinary.charAt(position) == '0') { iGapCurrently += 1; iTheZeros += 1; } else { if (iGapCurrently > iGapHigher) iGapHigher = iGapCurrently; iGapCurrently = 0; } if (iGapCurrently > iGapHigher) iGapHigher = iGapCurrently; } print("Binaryr: " + sBinary); print("Sequence of zeros: " + iTheZeros); return iGapHigher; } print("Number: " + iTheNumber); print("Gap Higher: " + binaryGap(iTheNumber));
run
|
edit
|
history
|
help
0
//Rhino 1.7.7.1print("Hello, world!")
Loops
GCD
Objects
tt
Wall
Sonic 1 + Sonic & Knuckles/Blue Spheres - Code Validator
JS exam part 2 - BPM
Javascript Basics
123