[2022-12-19 ๐Ÿซค TIL ]

Burkeyยท2022๋…„ 12์›” 19์ผ
0

TIL

๋ชฉ๋ก ๋ณด๊ธฐ
25/157

Udemy ์•Œ๊ณ ๋ฆฌ์ฆ˜ ๊ฐ•์˜์— ๋‚˜์˜ค๋Š” ๋ฌธ์ œ ํ’€์ด

์–ด๋ ค์šด๊ฑด ๋ชป ํ’€๊ณ  ๋„˜์–ด๊ฐ”๋‹ค... ์žฌ๊ท€ ์–ด๋ ต๋„ค..

function reverse (str) {
  result = str[str.length-1]
  if(str.length <= 1) {
    return result
  }
  result += reverse(str.slice(0, str.length-1))
  return result
}
  

console.log(reverse('abc')) // cba

์ธ์ˆ˜๋กœ ๋“ค์–ด์˜จ ๋ฌธ์ž์—ด์„ ๊ฑฐ๊พธ๋กœ ๋ฐ˜ํ™˜ํ•˜๋Š” ํ•จ์ˆ˜๋กœ ์„ ์–ธ

function someRecursive(arr, func){
  result = false
  if(func(arr[0])){
    result = true
    return result;
  }else{
    result = false
  }
  if (arr.length > 1){
    someRecursive(arr.slice(1), func)
  }
  return result
}

const isOdd = val => val % 2 !== 0;
console.log(someRecursive([1,2,3,4], isOdd)) // true
console.log(someRecursive([4,6,8,9], isOdd))// true
console.log(someRecursive([4,6,8], isOdd)) // false
console.log(someRecursive([4,6,8], val => val > 10)); // false

์ฒซ๋ฒˆ์งธ ์ธ์ˆ˜๋กœ ๋“ค์–ด์˜จ ๋ฐฐ์—ด ๊ฐ’๋“ค์ด ์ฝœ๋ฐฑ ํ•จ์ˆ˜์— ๋งž์ง€ ์•Š์€๊ฒŒ ํ•˜๋‚˜๋ผ๋„ ์žˆ์œผ๋ฉด true ๋ชจ๋‘ ํ•ด๋‹นํ•˜์ง€ ์•Š์œผ๋ฉด false๋ฅผ ๋ฐ˜ํ™˜ํ•˜๋Š” ํ•จ์ˆ˜๋ฅผ ์žฌ๊ท€๋ฅผ ์ด์šฉํ•˜์—ฌ ๊ตฌํ˜„


function capitalizeFirst (arr) {
  var result = []
  if(arr.length > 1){
    result.push(arr[0][0].toUpperCase() + arr[0].slice(1))
    result = result.concat(capitalizeFirst(arr.slice(1)))
    return result
  }
  result = result.concat(arr[0][0].toUpperCase() + arr[0].slice(1))
  return result
}

console.log(capitalizeFirst(['car','taco','banana'])); // ['Car','Taco','Banana']

์ธ์ˆ˜๋กœ ๋“ค์–ด์˜จ ๋ฐฐ์—ด์˜ ๋ฌธ์ž์—ด๋“ค์˜ ์ฒซ ๊ธ€์ž๋ฅผ ๋Œ€๋ฌธ์ž๋กœ ๋ณ€๊ฒฝํ•œ ๋ฐฐ์—ด๋กœ ์ถœ๋ ฅ

profile
์Šคํƒฏ ์˜ฌ๋ฆฌ๋Š” ์ค‘

0๊ฐœ์˜ ๋Œ“๊ธ€