JAVA SCRIPT 정리(2)

coc·2023년 8월 1일

(1) while문

1.while문은 조건만 부합하면 계속 반복(무한루프 오류가 일어날 확률증가)

2.while 안의 조건이 true이면 반복한다.

3.조건이 어느 상황에서도 true면 반복이 멈추지 않는다.

4.while은 반복 횟수가 정해지지 않은 경우 적합하다.

ex)

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
   
    <title></title>
    <style></style>
</head>
<body>

</body>
<script>
    var i =1;
    while(i<=10){//while 안의 조건이 true이면 반복한다.
        //반복할 내용
        //조건이 어느 상황에서도 true면 반복이 멈추지 않는다.
        console.log(i);
        i++;
        }
        //while은 반복 횟수가 정해지지 않은 경우 적합하다.
</script>
</html>

(2) do while

1.while 과 do-while은 기본적으로는 같은 값을 반환

2.조건을 벗어난 값이 들어왔을때 차이가 생긴다.

3.일단 한번 실행후 조건이 부합하지 않으면 더 이상 작동을 안 한다.

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
   
    <title></title>
    <style></style>
</head>
<body>

</body>
<script>
    var cnt = 11;
    while(cnt<10){
        cnt++;
    }
    console.log("while : " +cnt)

    cnt = 11;

    do{
        cnt++;
    }while(cnt<10);
    console.log("do while :" +cnt);

    //while 과 do-while은 기본적으로는 같은 값을 반환
    //조건을 벗어난 값이 들어왔을떄 차이가 생긴다.
</script>
</html>

(3) break

ex) 치킨 30마리 팔기 위해 준비했는데 갑자기 치킨이 20마리 밖에 주문이 안들어온다면 영업을 종료할 치킨을 제한수를 입력하여 영업을 종료 할 수 있게 만든다.

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
   
    <title></title>
    <style></style>
</head>
<body>

</body>
<script>
    //break는 진행중인 문장을 빠져나온다
    for(var i=0; i<30; i++){
        console.log("i : " +i);
        if(i==20){
            break;
        }
    }
</script>


</html>

(4) 함수 funtion

1.변수가 데이터를 담는 무언가 라면

2.함수는 동작을 실행해 주는 무언가 이다.

메서드 구조

funtion ex) 토스트기

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
   
    <title></title>
    <style></style>
</head>
<body>

</body>
<script>
    //토스트기
    function toaster(inp){
        console.log(input+"이 구워지고 있다.");
        return"구워진"+input;
    }

    var dish = "";//접시
    dish = toaster("식빵");//구워진걸 담아준다
    console.log(dish);


</script>

# 
</html>

(5) 배열(array)

1. 배열은 여러 개의 변수를 하나에 담는 기능을 한다.

2. 배열은 특정 기준 변수[0] 로 부터 얼마나 떨어져는가가 이름이 된다.

3. 배열의 크기는 거의 무한이다.

(6)배열(array) - function

1. Java script 에서는 배열을 다룰 수 있는 여러 함수를 제공 해 준다.

2. 대표적으로 push, pop, slice 등이 있다.

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
   
    <title></title>
    <style></style>
</head>
<body>
    <button onclick="arrPush()">push</button>
    <button onclick="arrPop()">pop</button>
    <button onclick="arrUnshift()">unshift</button>
    <button onclick="arrShift()">shift</button>

</body>
<script>
    var arr =[];
    var i = 1;

    function arrPush(){
        //console.log("click!!!!");
        arr.push(i); //뒤에서 데이터를 넣는다
        console.log(arr);
        i++;
    }
    function arrPop(){
        var num = arr.pop();//뒤에서 데이터를 빼낸다
        console.log("빼낸 값:" +num);
        console.log(arr);
    }
    function arrUnshift(){
        arr.unshift(i);// 앞에서 데이터를 넣는다
        i++
        console.log(arr);
    }
    function arrShift(){
        var num = arr.shift();// 앞에서 데이터 빼낸다
        console.log("빼낸 값:" +num);
        console.log(arr);
    }
</script>
</html>
profile
시작

0개의 댓글