//Write a recursive function called reverse which accpets a string and returns a new string in reverse.
function reverse(){
//
}
// reverse('awesome') // 'emosewa'
// reverse('rithmschool') // 'loohcsmhtir'
// Write a recursive function called isPalindrome which returns true
// if the string passed to it a palindrome (reads the same forward and backward).
// Otherwise it returns false.
// isPalindrome('awesome') // false
// isPalindrome('foobar') // false
// isPalindrome('tacocat') // true
// isPalindrome('amanaplanacanalpanama') // true
// isPalindrome('amanaplanacanalpandemonium') // false
function isPalindrome(){
//
}
// Write a recursive function called someRecursive which accepts an array and a callback.
// The function returns true if a single value in the array returns true when passed to the callback.
// Otherwise it returns false.
// Array.prototype.some()구현하기임
// [1,2,3,4].some(v => v % 2 !==0) //true
// SAMPLE INPUT / OUTPUT
// const isOdd = val => val % 2 !== 0;
// 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(){
//
}
// Write a recursive function called capitalizeFirst.
// Given an array of strings, capitalize the first letter of each string in the array.
function capitalizeFirst () {
}
// capitalizeFirst(['car','taco','banana']); // ['Car','Taco','Banana']
function nestedEvenSum (obj) {
let sum = 0;
for (let key in obj) {
if (typeof obj[key] === 'object') {
sum += nestedEvenSum(obj[key]);
} else if (typeof obj[key] === 'number' && obj[key] % 2 === 0){
sum += obj[key]
}
}
return sum;
}
// Write a recursivie function called capitalizeWords.
// Given an array of words, return a new array containing each word capitalized.
function capitalizeWords () {
//
}
capitalizeWords(['car', 'taco', 'banana']) // ['CAR', 'TACO', 'BANANA']
function capitalizeWords (array) {
if (array.length === 1) {
return [array[0].toUpperCase()];
}
let res = capitalizeWords(array.slice(0, -1));
res.push(array.slice(array.length-1)[0].toUpperCase());
return res;
}
capitalizeWords(['car', 'taco', 'banana']) // ['CAR', 'TACO', 'BANANA']
8. stringifyNumbers
// Write a function called stringifyNumbers which takes in an object
// and finds all of the values which are numbers and converts them to stirngs.
// Recursion would be a great way to solve this!
// (번역) 객체를 인자로 받고,
// 객체안의 모든 숫자인 value값을 찾고 문자로 바꾸는 함수를 만드시오.
// 재귀를 사용하시오.
let obj = {
num: 1,
test: [],
data: {
val: 4,
info: {
isRight: true,
random: 66
}
}
}
function stringifyNumbers(obj) {
//
}
stringifyNumbers(obj)
//{
// num: "1",
// test: [],
// data: {
// val: "4",
// info: {
// isRight: true,
// random: "66"
// }
// }
//}
function stringifyNumbers(obj) {
const answer = {};
for (let key in obj) {
//key 의 value가 숫자일 때
if (typeof obj[key] === 'number') {
answer[key] = obj[key].toString();
}
//key 의 value가 객체일 때
else if (typeof obj[key] === 'object' && !Array.isArray(obj[key])) {
answer[key] = stringifyNumbers(obj[key]);
}
//key 의 value가 배열일 때
else {
answer[key] = obj[key];
}
}
return answer;
}
9. collectStrings
version1 : pure function needed
version2 : helper function needed
// Write a function called collectStrings which accepts an object
// and returns an array of all the values in the object that have a typeof string
const obj = {
stuff: "foo",
data: {
val: {
thing: {
info: "bar",
moreInfo: {
evenMoreInfo: {
weMadeIt: "baz"
}
}
}
}
}
}
collectStrings(obj) // ["foo", "bar", "baz"])
function collectStrings() {
//
}