JSFiddle - React, Tailwind, and code Playground
by Jake Overall
JavaScript
//Coding Bat Challenges
var factorial = function (n) {
if (n < 0) {
return 'Please use whole numbers';
}
if (n <= 1) {
return 1;
}
return n * factorial(n - 1);
};
// How many ears per Bunny
var bunny = function (n) {
if (n <= 1) {
return 2;
}
return 2 + bunny(n - 1);
}
//sumDigits
var sumDigits = function (n) {
if (n < 10) {
return n
}
return n % 10 + sumDigits(Math.floor(n / 10));
}
//Count8 then double if following is 8
var count8 = function (n) {
if (n < 8) {
return 0;
}
if (n % 10 !== 8) {
return count8(Math.floor(n / 10));
} else if ((Math.floor(n / 10)) % 10 === 8) {
return 2 + count8(Math.floor(n / 10));
} else {
return 1 + count8(Math.floor(n / 10));
}
}
//powers to the rescue
var powerN = function (b, p) {
if (p === 0) {
return 1
}
return b * powerN(b, p - 1);
}
// string x counter recursion style
var xr = function (str) {
if (str.length < 1) {
return 0;
}
if (str.slice(0, 1) === 'x') {
return 1 + xr(str.slice(1));
} else {
return xr(str.slice(1));
}
}
// string x counter for style
var xf = function (str) {
var counter = 0;
for (var i = 0; i < str.length; i++) {
if (str.charAt(i) === 'x') {
counter++;
}
}
return counter;
}
//count hi occurances
var countHi = function (str) {
if (str.length <= 1) {
return 0;
}
if (str.slice(0, 2) === "hi") {
return 1 + countHi(str.slice(2))
} else {
return countHi(str.slice(1))
}
}
// replace x with y
var xToY = function (str) {
var y = str.slice(0, 1);
if (str.length < 1) {
return '';
}
if (y === 'x') {
y = 'y';
}
return y + xToY(str.slice(1));
}
//finding pie
var replacePie = function (str) {
var y = str.slice(0, 2);
if (str.length < 1) {
return '';
}
if (y === "pi") {
...