This file contains hidden or 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 alternatingSums(a) { | |
var sum = a.reduce(function(acc, val,currentIndex) { | |
if(currentIndex%2==0) | |
acc[0] += val; | |
else | |
acc[1] += val; | |
return acc; | |
}, [0,0]); | |
return sum; | |
} |
This file contains hidden or 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 flip(flipped){ | |
if(flipped.indexOf('(')>=0){ | |
var j=0; | |
var a=0; | |
var b=0; | |
var reversed=[]; | |
while (j<flipped.length&&b==0) { | |
if(flipped[j]=='('){ | |
a=j; | |
} |
This file contains hidden or 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 sortByHeight(a) { | |
var b=a.slice(0); | |
var x=0; | |
for (var i=0;i<b.length;i++){ | |
if(b[i]<0){ | |
b.splice(i,1); | |
} | |
} | |
b.sort((a,b)=>a-b); | |
for (var j=0;j<a.length;j++){ |
This file contains hidden or 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 numbers=n.toString().split(""); | |
var a=0,b=0; | |
for(i=0;i<numbers.length/2;i++){ | |
a+=parseInt(numbers[i],10); | |
b+=parseInt(numbers[numbers.length/2+i],10); | |
} | |
return a===b; | |
} |
This file contains hidden or 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 string1=s1.split(''); | |
var string2=s2.split(''); | |
var common=0; | |
for(var i=0;i<string1.length;i++){ | |
if(string2.indexOf(string1[i])>=0){ | |
common++; | |
string2.splice(string2.indexOf(string1[i]),1); | |
} |
This file contains hidden or 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 allLongestStrings(inputArray) { | |
var result=[""]; | |
var count=0; | |
for(var i=0;i<inputArray.length;i++){ | |
if(inputArray[i].length>=result[result.length-1].length){ | |
if(inputArray[i].length>result[result.length-1].length) | |
count=1; | |
else | |
count++ | |
result.push(inputArray[i]); |
This file contains hidden or 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 matrixElementsSum(matrix) { | |
price=0; | |
for(var y=0;y<matrix.length;y++) | |
for(var x=0;x<matrix[0].length;x++){ | |
if(y==0) | |
price+=matrix[y][x]; | |
else{ | |
if(matrix[y-1][x]==0) | |
matrix[y][x]=0; | |
price+=matrix[y][x]; |
This file contains hidden or 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 almostIncreasingSequence(sequence){ | |
var errors=0; | |
for(var i=0;i+1<sequence.length&&errors<2;i++) | |
if(sequence[i]>=sequence[i+1]){ | |
errors++; | |
if(i+2<sequence.length&&i>0) | |
if(sequence[i]>=sequence[i+2]) | |
errors++; | |
} | |
return errors<=1; |
This file contains hidden or 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 isSmooth(arr) { | |
return (arr.length%2==0) ? arr[0]===arr[arr.length-1]&&arr[0]===(arr[arr.length/2-1]+arr[arr.length/2]):arr[0]==arr[arr.length-1]&&arr[0]==(arr[Math.floor(arr.length/2)]); | |
} |
This file contains hidden or 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 makeArrayConsecutive2(statues) { | |
statues=statues.sort((a,b)=>a-b); | |
return(statues[statues.length-1]-statues[0]+1-statues.length); | |
} |