This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function binarySearch(Array,startIndex, endIndex, x){ | |
middleIndex= Math.floor((startIndex + endIndex)/2); | |
if(startIndex > endIndex) | |
return -1; | |
if (x == Array[middleIndex]) | |
return middleIndex; | |
else if (x < Array[middleIndex]) | |
return binarySearch(Array, startIndex, middleIndex -1, x); | |
else | |
return binarySearch(Array, middleIndex + 1, endIndex, x); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function isLucky(n) { | |
var sumLeft = 0; | |
var sumRight = 0; | |
digits= n.toString().split('');//to make an array of characters, at first change the numbers to string and then use split() method. | |
ArrayNum = digits.map(Number);//we have an array of strings and want to change the type to numbers so we use .map() method. | |
len = ArrayNum.length; | |
if(len % 2 !==0){ | |
return false;//check if the "n" has even number of digits or not. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function commonCharacterCount(s1, s2){ | |
var e=0; | |
ArrayS1 = s1.split('');//This method changes the string to an array. | |
ArrayS2 = s2.split(''); | |
for(var i=0; i<ArrayS1.length;i++){ | |
for(var j=0; j<ArrayS2.length; j++){ | |
if(ArrayS1[i]==ArrayS2[j]){ | |
e++; | |
ArrayS1.splice(i,1);//This method removes the ith and jth elements which are equal | |
ArrayS2.splice(j,1); |