Skip to content

Instantly share code, notes, and snippets.

View Underdoge's full-sized avatar
🏠
Working from home

Underdoge Underdoge

🏠
Working from home
View GitHub Profile
@Underdoge
Underdoge / adjacentElementsProduct.js
Last active February 24, 2017 02:26
adjacentElementsProduct
function adjacentElementsProduct(inputArray) {
var i=0,x=inputArray[i]*inputArray[i+1];
i++;
while(i+1<inputArray.length){
if(inputArray[i]*inputArray[i+1]>x)
x=inputArray[i]*inputArray[i+1];
i++;
}
return x;
}
@Underdoge
Underdoge / shapeArea.js
Last active April 7, 2021 23:13
shapeArea
function shapeArea(n) {
return 2*Math.pow(n,2) - 2*n +1;
}
@Underdoge
Underdoge / makeArrayConsecutive2.js
Last active February 24, 2017 04:26
Make Array Consecutive 2
function makeArrayConsecutive2(statues) {
statues=statues.sort((a,b)=>a-b);
return(statues[statues.length-1]-statues[0]+1-statues.length);
}
@Underdoge
Underdoge / isSmooth.js
Created February 24, 2017 22:49
isSmooth
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)]);
}
@Underdoge
Underdoge / almostIncreasingSequence.js
Created February 25, 2017 22:23
almostIncreasingSequence
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;
@Underdoge
Underdoge / matrixElementsSum.js
Created February 25, 2017 22:36
matrixElementsSum
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];
@Underdoge
Underdoge / allLongestStrings.js
Created February 25, 2017 23:09
allLongestStrings
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]);
@Underdoge
Underdoge / commonCharacterCount.js
Created February 25, 2017 23:26
commonCharacterCount
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);
}
@Underdoge
Underdoge / isLucky.js
Created February 26, 2017 07:43
isLucky
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;
}
@Underdoge
Underdoge / sortByHeight.js
Created February 26, 2017 08:18
sortByHeight
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++){