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 / extractEachKth.js
Created March 7, 2017 04:44
extractEachKth
function extractEachKth(inputArray, k) {
return inputArray.filter((curr,index)=>{
if((index+1)%k!=0)
return curr;
});
}
@Underdoge
Underdoge / stringsRearrangement.js
Last active March 7, 2017 01:59
stringsRearrangement
function isNext(string1,string2){
var diff=0;
for(var i=0;i<string1.length;i++){
if(string1[i]!=string2[i])
diff++;
}
if (diff!=1)
return false;
else
return true;
@Underdoge
Underdoge / absoluteValuesSumMinimization.js
Created March 6, 2017 06:33
absoluteValuesSumMinimization
function absoluteValuesSumMinimization(a) {
var x=a[0],temp=a.reduce((acc,curr)=>{
return acc+Math.abs(curr-x);
},0);
for(var i=1;i<a.length;i++){
if(a.reduce((acc,curr)=>{return acc+Math.abs(curr-a[i]);},0)<temp){
temp=a.reduce((acc,curr)=>{return acc+Math.abs(curr-a[i]);},0);
x=a[i];
}
}
@Underdoge
Underdoge / depositProfit.js
Created March 6, 2017 06:00
depositProfit
function depositProfit(deposit, rate, threshold) {
var years=0;
while(deposit<threshold){
deposit+=deposit*(rate/100);
years++;
}
return years;
}
@Underdoge
Underdoge / circleOfNumbers.js
Created March 6, 2017 05:54
circleOfNumbers
function circleOfNumbers(n, firstNumber) {
return (firstNumber*2==n)?0:(firstNumber<(n/2))?firstNumber+(n/2):firstNumber-(n/2);
}
@Underdoge
Underdoge / chessBoardCellColor.js
Created March 6, 2017 05:14
chessBoardCellColor
function chessBoardCellColor(cell1, cell2) {
if((cell1.charCodeAt(0)-64)%2==0&&(cell2.charCodeAt(0)-64)%2==0)
if((cell1.charCodeAt(1)%2!=0&&cell2.charCodeAt(1)%2!=0)||(cell1.charCodeAt(1)%2==0&&cell2.charCodeAt(1)%2==0))
return true;
else
return false;
else
if((cell1.charCodeAt(0)-64)%2!=0&&(cell2.charCodeAt(0)-64)%2!=0)
if((cell1.charCodeAt(1)%2==0&&cell2.charCodeAt(1)%2==0)||(cell1.charCodeAt(1)%2!=0&&cell2.charCodeAt(1)%2!=0))
return true;
@Underdoge
Underdoge / alphabeticShift.js
Created March 6, 2017 04:19
alphabeticShift
function alphabeticShift(inputString) {
var shift=inputString.split("");
var shifted=shift.map((curr)=>{
if (curr=='z')
return 'a';
else
return String.fromCharCode(curr.charCodeAt(0)+1);
})
return shifted.join("");
}
@Underdoge
Underdoge / variableName.js
Created March 6, 2017 03:36
variableName
function variableName(name) {
return /^([a-z]|[A-Z]|_)([a-z]|[A-Z]|_|\d)*$/.test(name);
}
@Underdoge
Underdoge / evenDigitsOnly.js
Created March 6, 2017 03:08
evenDigitsOnly
function evenDigitsOnly(n) {
var nums=n.toString().split("");
return nums.reduce((acc,currval,currindex,arr)=>{
return acc&&currval%2==0
},true);
}
@Underdoge
Underdoge / arrayReplace.js
Created March 6, 2017 03:01
arrayReplace
function arrayReplace(inputArray, elemToReplace, substitutionElem) {
return inputArray.map((curr)=>{
if(curr==elemToReplace)
return substitutionElem;
else
return curr;
});
}