Created
June 19, 2016 05:50
-
-
Save hackerdem/1571332d637e50fdde16c1a4df20f885 to your computer and use it in GitHub Desktop.
solutions for codewars challenges in javascript
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
//Rearange Number to Get its Maximum | |
var maxRedigit = function(num) { | |
var ou=[]; | |
var snum=num.toString(); | |
if (num<=0 || snum.length!=3){return null} | |
for (var i=0,len=snum.length;i<len;i+=1){ | |
ou.push(+snum.charAt(i));} | |
var a=ou.sort(function(a,b){return b-a}).join(''); | |
return parseInt(a) | |
}; | |
//Sir , showMe yourID | |
function showMe(yourID){ | |
var s=yourID.match(/^[0-9-a-z-]|-[a-z]|-$|\s/) | |
if (s==null){return true;}else{return false;} | |
} | |
//The Coupon Code | |
function checkCoupon(enteredCode, correctCode, currentDate, expirationDate){ | |
var curdate=new Date(currentDate); | |
var exdate=new Date(expirationDate); | |
if(enteredCode!==correctCode || curdate>exdate){return false}else{return true} | |
} | |
//Name on billboard | |
function billboard(name, price = 30){ | |
var a=name.length; | |
var tot=0; | |
for(i=1;i<=a;i++){ | |
tot+=price; | |
} | |
return tot | |
} | |
//How old will I be in 2099? | |
function calculateAge(p,r) { | |
if (r-p>1){ | |
return "You are "+(r-p)+" years old." | |
}else if(r===p-1){ | |
return "You will be born in "+(p-r)+" year." | |
}else if(r<p){ | |
return "You will be born in "+(p-r)+" years." | |
}else if(p===r-1){ | |
return "You are "+(r-p)+" year old." | |
}else{ | |
return "You were born this very year!" | |
} | |
} | |
//Fix your code before the garden dies! | |
function rainAmount(mm){ | |
if (mm < 40) { | |
return "You need to give your plant " + Math.abs(40-mm) + "mm of water" | |
}else { | |
return "Your plant has had more than enough water for today!" | |
}; | |
} | |
//Sum of positive | |
function positiveSum(arr) { | |
var sum=0 | |
for(i=0;i<arr.length;i++){ | |
if (arr[i]>=0){sum=sum+arr[i]} | |
} | |
return sum | |
} | |
//Heads and Legs | |
function animals(heads, legs){ | |
var x=(4*heads-legs)/2; | |
var y=heads-x; | |
if (x<0 || y<0){return 'No solutions'} | |
if (x===Math.round(Math.abs(x))){return [x,heads-x]}else{return 'No solutions'} | |
} | |
//Match My Husband | |
function match(usefulness,months){ | |
var sum=usefulness.reduce(function(a,b){return a+b;},0); | |
var grvt=100*Math.pow(Math.E,-0.15*months); | |
if (sum>=Math.round(grvt,3)){return 'Match!';}else{return 'No match!';} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment