Lumpy Space Princess - Adventure Time
공부/Javascript

[JS] 노마드코더 - 바닐라JS로 크롬 앱 만들기 (2.8 ~ 2.16)

yaebb_82 2022. 7. 30.

 

 

 

- argument(인수)

: function을 실행하는 동안 어떤 정보를 function에게 보낼 수 있는 방법

(인수는 여러 개를 사용 O / 단, 순서 중요!)

function sayHello(nameOfPerson, age){
    console.log("Hello my name is " + nameOfPerson + "and I'm " + age);
}

sayHello("nova", 10);
sayHello("shin", 20);
sayHello("yb", 30);

cf.) NaN 타입 : Not a Number (숫자가 아니다)

 

 

 

객체 안에서 function 사용하기 (객체 안에서 function을 사용할 때는 형태가 조금 다르다.)

const player = {
    name: "nova",
    sayHello: function(otherPersonName){
        console.log("hello! " + otherPersonName);
    }
}

console.log(player.name);
player.sayHello("peter");

 

 

 

console.log() 사용하지 않고, 코드 안에서 결과 표시하기

return(반환)을 하지 않고 console.log()를 찍어주면 undefined로 값이 출력되지 않는다.

 

fuction안에서 return을 해주면 calAge(age)를 대체해준다.

const age = 96;
function calAge(ageOfForeigner){
    return ageOfForeigner + 2;
}

const krAge = calAge(age);

console.log(krAge);

 

 

 

- Conditionals(조건문)   중요!!

   • prompt : 값을 입력할 수 있도록 창을 띄워준다. (하지만 css 적용을 할 수 없어 잘 사용하지 않는다.)

   • typeof : 타입을 확인할 수 있다.

   • parseInt : string → number 로 타입 변환해주는 함수

   • isNan : is Not a Number → T / F 를 return

const age = parseInt(prompt("How old are you?"));

console.log(isNaN(age));

 

 

cf.) condition(조건)은 boolean 이어야 한다.

const age = parseInt(prompt("How old are you?"));

if(isNaN(age)){
    console.log("Please write a number.");
} else{
    console.log("Oh, your age is "+ age);
}

 

 

 

- if - else if - else 조건문 사용하기

   • >, <, ≥, ≤ 와 같이 비교연산자를 활용하여 조건 나타낼 수 있다.

   • 조건에서 &&(AND), ||(OR) 사용해서 나타낼 수도 있다.

   • === 는 값이 일치하는지 비교할 수 있다.

const age = parseInt(prompt("How old are you?"));

if(isNaN(age)){
    console.log("Please write a number.");
} else if(age<18){
    console.log("You are too young.");
} else{
    console.log("You can drink!");
}

20 입력 시, 코드 실행 결과

 

 

 

반응형

댓글