슈피치 테크블로그
article thumbnail
🍋 [Algorithm] Challenges - Recursion 3️⃣
🍒 Knowledge/Algorithm 2023. 5. 18. 12:07

🍋 Challenges - Recursion 3️⃣ Example : capitalizeFirst Question 문자열을 요소로 가진 배열을 매개변수로 요구하는 재귀 함수를 작성한다. 함수는 각 문자열의 첫번째 글자를 대문자로 변환한 문자열 배열을 반환한다. capitalizeFirst(['car', 'taco', 'banana']); // ['Car', 'Taco', 'Banana'] My solution function capitalizeFirst(strs) { if (strs.length === 0) return []; let upperStr = ''; for (let i = 0; i < strs[0].length; i++) { if (i === 0) upperStr = strs[0][i].toU..

article thumbnail
🍋 [Algorithm] Challenges - Recursion 2️⃣
🍒 Knowledge/Algorithm 2023. 5. 17. 15:42

🍋 Challenges - Recursion 2️⃣ Example : reverse Question 문자열을 매개변수로 요구하는 재귀 함수를 작성한다. 이 함수는 인자로 들어온 문자열을 반대로 출력한다. reverse('awesome'); // emosewa reverse('rithmschool'); // loohcsmhtir My solution function reverse(string) { if (string.length === 1) return string[0]; return string[string.length - 1] + reverse(string.slice(0, -1)); } Reference solution function reverse(str){ if(str.length val > 10)..

article thumbnail
🍋 [Algorithm] Challenges - Recursion 1️⃣
🍒 Knowledge/Algorithm 2023. 5. 17. 14:20

🍋 Challenges - Recursion 1️⃣ Example : power Question 밑과 지수를 매개변수로 요구하는 함수를 작성한다. 밑의 지수에 해당하는 거듭제곱을 반환해야하며 Math.pow()는 사용이 금지된다. (같은 기능을 수행해야 한다.) power(2, 0); // 1 power(2, 2); // 4 power(2, 4); // 16 My solution function power(base, exponent) { if (exponent === 0) return 1; return base * power(base, exponent - 1); } Reference solution function power(base, exponent){ if(exponent === 0) return 1..

article thumbnail
🍋 [Algorithm] Recursion - Helper Method
🍒 Knowledge/Algorithm 2023. 4. 27. 17:14

🍋 Recursion - Helper Method Helper Method pattern 외부 함수에 내부 재귀 함수를 정의하여 배열이나 데이터 목록 같은 걸 컴파일 할 때 사용하는 패턴 function outer(input) { let outerScopedVariable = []; function helper(helperInput) { // modify the outerScopedVariable helper(helperInput--); } helper(input); return outerScopedVariable; } Example : collectOddValues Question collectOddValues([1, 2, 3, 4, 5]); // [1, 3, 5] Pure recursion solut..

article thumbnail
🍋 [Algorithm] Recursion - Examples
🍒 Knowledge/Algorithm 2023. 4. 27. 15:45

🍋 Recursion - Examples Example : countDown Question countDown(5); /* 5 4 3 2 1 All done! */ Loop solution function countDown(num) { for (let i = num; i > 0; i--) { console.log(i); } console.log("All done!"); } Recursion solution function countDown(num) { if (num

article thumbnail
🍋 [Algorithm] Recursion
🍒 Knowledge/Algorithm 2023. 4. 18. 23:00

🍋 Recursion Recursion 주어진 문제를 해결하기 위해 하나의 함수에서 자신을 다시 호출하여 작업을 수행하는 방식이다. 한 가지 문제를 가지고 어떤 엔드 포인트에 도달할 때까지 더 작은 부분을 반복적으로 수행한다. 이 때 해당 엔드 포인트를 베이스 케이스라고 부른다. 🧒🏻 : 목록의 숫자들이 홀수인지 알려주세요. 🐲 : 난 목록의 첫 번째 숫자만 알려줄 것이다. 🧒🏻 : (3142 5798 6550 5914) 이 목록의 첫 번째 숫자는 홀수인가요? 🐲 : 그 숫자는 짝수이다. 🧒🏻 : (5798 6550 5914) 이 목록의 첫 번째 숫자는 홀수인가요? 🐲 : 그 숫자는 짝수이다. 🧒🏻 : (6550 5914) 이 목록의 첫 번째 숫자는 홀수인가요? 🐲 : 그 숫자는 짝수이다. 🧒🏻 : (5..

profile on loading

Loading...