function reverse(str){
let result = '';
function helper(helperInput, index) {
if (index === helperInput.length) {
result += helperInput.slice(0, 1);
return;
}
if (index === 0) {
result += helperInput.slice(-1);
} else {
result += helperInput.slice(-index, -index + 1);
}
helper(helperInput, ++index);
}
helper(str, 0);
return result;
}
function reverse(str){
if (str.length <= 1) return str;
return reverse(str.slice(1)) + str[0];
}
function isPalindrome(str){
if (str.length <= 1) {
return true;
}
if (str[0] !== str[str.length -1]) {
return false;
}
return isPalindrome(str.slice(1, -1))
}
function isPalindrome(str){
if(str.length === 1) return true;
if(str.length === 2) return str[0] === str[1];
if(str[0] === str.slice(-1)) return isPalindrome(str.slice(1,-1))
return false;
}
// someRecursive([1,2,3,4], isOdd) // true
// someRecursive([4,6,8,9], isOdd) // true
// someRecursive([4,6,8], isOdd) // false
// someRecursive([4,6,8], val => val > 10); // false
function someRecursive(arr, callback){
if (arr.length === 0) {
return false;
}
if (callback(arr[0])) {
return true;
}
return someRecursive(arr.slice(1), callback);
}
// flatten([1, 2, 3, [4, 5] ]) // [1, 2, 3, 4, 5]
// flatten([1, [2, [3, 4], [[5]]]]) // [1, 2, 3, 4, 5]
function flatten(arr){
let result = [];
function helper(helperInput) {
if (helperInput.length === 0) {
return;
}
if (typeof helperInput[0] === 'object') {
helper(helperInput[0]);
} else {
result.push(helperInput[0]);
}
helper(helperInput.slice(1));
}
helper(arr);
return result;
}
function flatten(oldArr) {
let newArr = [];
for (let i = 0; i < oldArr.length; i++) {
if (Array.isArray(oldArr[i]) {
newArr = newArr.concat(flatten(oldArr[i]));
} else {
newArr.push(oldArr[i]);
}
}
return newArr;
}
접근 방법
// capitalizeFirst(['car','taco','banana']); // ['Car','Taco','Banana']
function capitalizeFirst (arr) {
let newArr = [];
if (arr.length === 0) {
return newArr;
}
const letter = arr[0];
const capitalLetter = letter.slice(0,1).toUpperCase();
const restLetter = letter.slice(1);
const newLetter = capitalLetter + restLetter;
newArr.push(newLetter);
return newArr.concat(capitalizeFirst(arr.slice(1)));
}
function capitalizeFirst (array) {
if (array.length === 1) {
return [array[0][0].toUpperCase() + array[0].substr(1)];
}
const res = capitalizeFirst(array.slice(0, -1));
const string = array.slice(array.length - 1)[0][0].toUpperCase() + array.slice(array.length-1)[0].substr(1);
res.push(string);
return res;
}