Search
 
SCRIPT & CODE EXAMPLE
 

JAVASCRIPT

Getting Binary gap in JS

let N = 9;
const Binary = N.toString(2); // "1001"
const BinaryArray = Binary.split(''); // ["1", "0", "0", "1"]
// finding the first one via its index
const firstOne = BinaryArray.indexOf("1"); // 0 = Index of array
// new array created taking a slice of original array 
// from the index of the firstOne + 1 index
let NewBinaryArray = BinaryArray.slice(firstOne + 1); 
// ["0", "0", "1"]
// finding second one via its index in new array slice
const secondOne = NewBinaryArray.indexOf("1"); // 2
// where we store our gaps
const gaps = [];
// adding 2 to our gaps array
gaps.push(secondOne); // [2]
// getting largest value in array
return Math.max.apply(Math, gaps); // 2
Comment

PREVIOUS NEXT
Code Example
Javascript :: js getelementbyid 
Javascript :: angular bind checkbox 
Javascript :: array.find is not a function 
Javascript :: javascript sort array by object property 
Javascript :: array to excel javascript 
Javascript :: discord.js on ready 
Javascript :: can i pass data with usenavigate react router 
Javascript :: react native text get number of lines 
Javascript :: contains whitespace js function 
Javascript :: loop array in javascript 
Javascript :: node js server get images from folder 
Javascript :: vue event focus out 
Javascript :: json enum string 
Javascript :: javascript multiply array with scalar 
Javascript :: last element in javascript 
Javascript :: jquery loop through collection backwards 
Javascript :: update array of object using other array javascript 
Javascript :: object length javascript 
Javascript :: vaidate youtube url 
Javascript :: viewchild for ngfor 
Javascript :: how to append the dropdown values by jquery each function 
Javascript :: how to take input in javascript in coding 
Javascript :: js event preventdefault continue 
Javascript :: vuex use state in action 
Javascript :: error vuejs from chokidar enospc 
Javascript :: unshift 
Javascript :: react check if mounted 
Javascript :: javascript absolute path 
Javascript :: material ui color background 
Javascript :: get the first word of a string javascript 
ADD CONTENT
Topic
Content
Source link
Name
9+2 =