728x90
참고자료: 노마드 코더 바닐라 JS로 크롬 앱 만들기 https://nomadcoders.co/javascript-for-beginners/lobby
7-8. Functions part One, Two
function sayHello(nameOfPerson, age) {
console.log("Hello! My name is " + nameOfPerson + " and I'm " + age);
}
sayHello("yeram", 24);
sayHello("sehyeon", 26);
👉🏻함수는 코드를 캡슐화한다.
👉🏻함수를 호출 할 때 괄호 안에 인자를 넣어 보낸다. 인자는 여러개 보낼 수 있다.
👉🏻인자로 보낸 변수는 해당 함수 내에서만 유효하다.
11. Returns
const age = 96;
function calculateKrAge(ageOfForeigner) {
return ageOfForeigner + 2;
}
const krAge = calculateKrAge(age); // 98
👉🏻return을 사용하면 함수 결과값을 변수에 정의할 수 있다. 즉, 함수 외부에서 함수의 결과를 사용할 수 있다.
13-15. Conditionals part One-Three
const age = parseInt(prompt("How old are you?"));
if (isNaN(age) || age < 0) {
console.log("Please write a real positive number.");
} else if (age < 18) {
console.log("You are too young.");
} else if (age >= 18 && age <= 50) {
console.log("You can drink.");
} else if (age >= 51 && age <= 80) {
console.log("You should exercise.");
} else if (age > 80) {
console.log("You can do whatever you want.");
} else {
console.log("You can't drink.");
}
👉🏻parseInt로 string으로 받은 숫자를 정수로 바꿀 수 있다.
👉🏻prompt로 알림창을 띄울 수 있다.
👉🏻isNaN으로 숫자인지 아닌지를 판단할 수 있다.
👉🏻&&와 ||를 and와 or 연산자로 사용할 수 있다.
728x90
'그 땐 Front했지 > 그 땐 JavaScript했지' 카테고리의 다른 글
[Nomad/바닐라 JS Challenges] 8일차 | Ch01 QUOTES AND BACKGROUND (0) | 2022.06.16 |
---|---|
[Nomad/바닐라 JS Challenges] 8일차 | Ch05 CLOCK (0) | 2022.06.15 |
[Nomad/바닐라 JS Challenges] 5일차 & 6일차 | Ch03 JAVASCRIPT ON THE BROWSER (0) | 2022.06.11 |
[Nomad/바닐라 JS Challenges] 2일차 | Ch02 WELCOME TO JAVASCRIPT (0) | 2022.06.08 |
[Nomad/바닐라 JS Challenges] 1일차 | Ch01 INTRODUCTION (0) | 2022.06.07 |