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 / 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 / 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++){
@Underdoge
Underdoge / reverseParentheses.js
Created February 27, 2017 19:11
reverseParentheses
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;
}
@Underdoge
Underdoge / alternatingSums.js
Last active February 27, 2017 22:31
alternatingSums
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;
}
@Underdoge
Underdoge / addBorder.js
Created February 28, 2017 05:51
addBorder
function returnLine(text){
var line=["*"];
var letters=text.split("");
for(var i=0;i<letters.length;i++){
line.push(letters[i]);
}
line.push("*");
return line.join("");
}