# 함수 문제 <br> # 문제 1 ```jsx function greet(){ console.log("안녕 내 이름은 제시카야"); }; greet(); /* 결과 안녕 내 이름은 제시카야 */ ``` <br> # 문제 2 ```jsx function greet(name){ console.log(`안녕 내 이름은 ${name}야`); } greet("할리"); /* 결과 안녕 내 이름은 할리야 */ ``` <br> # 문제 3 ```jsx function greet(name){ return name; } let userName = greet("에밀리"); console.log(`유저의 이름은 ${userName}입니다`); /* 결과 유저의 이름은 에밀리입니다 */ ``` <br> # 문제 4 ```jsx function meetAt(year, month, day){ if(year&&month&&day){ return `${year}/${month}/${day}`; } else if (year&&month){ return `${year}년 ${month}월`; } else if (year){ return `${year}년`; } } console.log(meetAt(2024)); ``` - meetAt(2024) 를 호출했을 때 마지막 else if 문만 리턴되는 이유 ? - meetAt (2024) 를 호출하면 year 에는 2024 가 할당되고, **month 와 day 에는 undefined 가 할당됨** - 조건 평가에서 month 와 day 는 거짓으로 평가됨 - 따라서 마지막 else if 조건만 true 가 되어, 리턴됨 - if (year) { … } 에서는 **( ) 의 값이 truthy 일 때 실행**된다. - 즉, year 이 **undefined 가 아니라 숫자값이라면** , 출력됨 - if (year && month && day ) { .. } 에서는 year, month, day 모두가 truthy 일 때 = 셋 모두가 undefined 가 아닐 때 ⇒ 모두 숫자일 때 (문제 상) 실행됨 ### Truthy 와 Falsy > 보통 프로그래밍 언어에서는 boolean 값이 true, false 로만 존재한다. 그러나 자바스크립트에는 Truthy 와 Falsy 라는 새로운 개념이 또 존재한다. > ![image](https://hackmd.io/_uploads/rJVpdSnLR.png) - 주의할 점 - Truthy 의 빈 객체, 빈 배열은 Falsy 로 착각하는 경우가 많음 - 위 값들을 **강제로 `boolean` (true or false) 로 변경**하는 법 - **! 연산자를 두 번 연속해서 사용**한다 ```jsx let value = "hello"; console.log(!!value); // 출력 : true ``` - 첫 번째 ! 에서 hello 를 Falsy 값인 false 로 바꿈 - 두 번째 ! 에서 false 를 원래대로 true 로 변환함 <br> # 문제 5 ``` function findSmallestElement(arr){ if(arr.length == 0){ return 0; } else { let min = arr[0]; for (let item of arr){ if (min > item){ min = item; } } return min; } } console.log(findSmallestElement([10,-2,3,4,-1])); ``` <br> # 문제 6 ```jsx function moneyCount(money){ let fiveMill = Math.floor(money/50000); console.log(`50000 * ${fiveMill}`); let oneMill = Math.floor((money % 50000)/10000); console.log(`10000 * ${oneMill}`); let fiveThou = Math.floor((money%10000)/5000); console.log(`5000 * ${fiveThou}`); let thousand = Math.floor((money%5000)/1000); console.log(`1000 * ${thousand}`); let fiveHund = Math.floor((money%1000)/500); console.log(`500 * ${fiveHund}`); let hundred = Math.floor((money%500)/100); console.log(`100 * ${hundred}`); } moneyCount(162300); ```