CSS&JS/👀Study and Copy

[JS]이진법/십진법 재귀함수

arancia_ 2022. 1. 24. 12:59

 

제로초님의 렛츠기릿  자바스크립트 강의에 Eun-Gil Cho님이 작성해주신 질문을 바탕으로 쓰여진 포스팅입니다.

재귀함수로 십진수를 이진수로 표현하기

See the Pen Untitled by Oh Ikmyeong (@dpffpself) on CodePen.

 

 

이건 그냥 이진수를 십진수로 바꾸기

See the Pen To Decimal by Oh Ikmyeong (@dpffpself) on CodePen.

 




얄팍한 코딩사전 - 재귀의 기초 이해

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Factorial</title>
</head>
<body>
    <a href="https://velog.io/@sominpark/재귀의-기초-이해" target="_blank">재귀의-기초-이해</a>
    <a href="https://www.youtube.com/watch?v=aPYE0anPZqI" target="_blank">얄팍한 코딩사전 유튜브</a>
    <h1>팩토리얼 해조</h1>
    <form id="myForm"><input type="number" value="5"/></form>
    
    <h3>이곳에 결과표시</h3>
    <div id="result"></div>
<script>
    const $myForm = document.getElementById('myForm');
    const $result = document.getElementById('result');
 
    $myForm.addEventListener('submit',(e)=>{
        e.preventDefault();
        const iptVal = e.target[0].value;
        const result = factorial(iptVal);
        $result.textContent = result;
    });
 
    function factorial(val){
        if(val <= 1){return val;}
        return val * factorial(val - 1);
    }//factorial
</script>
<script>
    const arr = [1,4,5,3,2];
 
    function recursive_sum(array){
        if(array.length <= 1){return array[0];}
        return array[0+ recursive_sum(array.slice(1));
    }
    
    //단순한거라면 reduce가 낫지
    console.log(arr.reduce((prev,next)=>prev + next));
    console.log(recursive_sum(arr));
</script>
<script>
    const boss = [3,[1,3,[3,[6,2],5],1,3],4, [8,1, [2,1,9],5], 5,9]; //ㅋㅋㄹㅃㅃ
    
    function deep_recursive_sum(start, array){
        if(array.length === 0){return start;}
 
        return deep_recursive_sum(
            start + (typeof array[0=== 'number' ? array[0] : deep_recursive_sum(0,array[0])),
            array.slice(1)
        );
    }//d
    
    console.log(deep_recursive_sum(0,boss)); //71
</script>
</body>
</html>
cs

 



얄팍한 코딩사전 - 하노이의 탑 (5:25)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>하노이의탑</title>
</head>
<body>
 
    <h1>하노이의 탑</h1>
    <a href="https://www.youtube.com/watch?v=aPYE0anPZqI" target="_blank">5:25</a>
 
<script>
    //최종이 가운데임
    let i = 0;
    function hanoi(num,from,to,other){
        if(num == 0){return}
        
        hanoi(num - 1from, other, to);
        console.log(`${i++} : ${num}을 ${leftCenterRight(from)}에서 ${leftCenterRight(to)}로 옮긴다.`);
        hanoi(num - 1, other, to, from);
    }
 
    function leftCenterRight(number){
        switch(number){
            case 0 : return "처음";
            case 1 : return "가운데";
            case 2 : return "끝";
        }
    }
 
    hanoi(4,0,1,2);
 
</script>
</body>
</html>
cs

 

얄팍한 코딩사전 - 꼬리재귀 조건

/* 꼬리재귀 가능 - 함수자체만 return 할 때 */
function can_tail_recurse(arg){
    //....
    return can_tail_recurse(arg);
}


/* 응 안돼 네 메모리 스택 폭발시킬래 ㅋㅋㄹㅃㅃ */
function cant(arg){
    let n;
    //....
    return n * cant(arg);
}