Winnie The Pooh Bear 자바스크립트 시험 3 오답

배움기록/JAVASCRIPT

자바스크립트 시험 3 오답

코딩은 처음이라 2023. 3. 17. 10:41

“ 지연되는 프로젝트에 인력을 더 투입하면 오히려 더 늦어진다. ”

- Frederick Philips Brooks
Mythical Man-Month 저자
728x90
반응형

자바스크립트 시험 3 오답

 

 

 

01.

{
    (function(){
        console.log("함수가 실행되었습니다.");
    })();
}
✨ 정답보기
 
함수가 실행되었습니다.

02.

{
    function func(str = "함수가 실행되었습니다."){
        document.write(str);
    }
    func();
}
✨ 정답보기
 
함수가 실행되었습니다.

 

03.(X)

{
    let sum = 0;
    for(var i=1; i<=10; i+=2) {
        sum += i;
    };
    document.write(sum);
}
✨ 정답보기
 
25
 
 
📑 풀이
 
i를 1부터 10까지 2씩 증가시키면서 반복
반복문 안에서 sum 변수에 i 값을 더해줌
1부터 10까지 홀수의 합이 저장
함수를 사용해 출력.
 
 
 

04.

{
    const obj = {
        a: 100,
        b: 200,
        c: "javascript"
    }
    const { a, b, c } = _______;

    document.write(a);
    document.write(b);
    document.write(c);

    //100
    //200
    //javascript
}
✨ 정답보기
 
obj

05.

{
    const objA = {
        a: 100,
        b: 200
    }
    const objB = {
        c: "javascript",
        d: "jquery"
    }
    const spread = {______, ______}

    document.write(spread.a);
    document.write(spread.b);
    document.write(spread.c);
    document.write(spread.d);

    //100
    //200
    //javascript
    //jquery
}
✨ 정답보기
 
...objA, ...objB

06.

{
    if( _____ ){
        document.write("조건문이 실행되었습니다.(true)");
    } else {
        document.write("조건문이 실행되었습니다.(false)");
    }

    //document.write("조건문이 실행되었습니다.(false)");
    //보기
    //true, false, 1, "1", "", 0, null, undefined, [], {}
}
✨ 정답보기
 
false, 0, ""(빈문자열), null, undefined

07.(X)

{
    if( num == 100 ){
        document.write("true");
    } else {
    	document.write("false");
    }
}
✨ 정답보기
 
num == 100 ? document.write("true") : document.write("false");
 
 
📑 풀이
 
else if 문 대신에 간단한 조건식을 사용할 수 있게 해주는 것이기 때문에
if와 else를 생략!
 
앞에 if를 붙여서 틀렸다..
if 생략!!
 
 

08.

{
    for(var i=1; i<=1; i++){
        document.write(i);
        for(var j=1; j<=5; j++){
            document.write(j);
        }
    }
}
✨ 정답보기
 
1/1,2,3,4,5

09.

{
    const num = [100, 200, 300, 400, 500];

    for(let i=0; i<num.length; i++){
        document.write(_______);
    }

    //100 200 300 400 500
}
✨ 정답보기
 
num

10.

{
    const num = [100, 200, 300, 400, 500];

    num.forEach(function(el){
        document.write(________);
    });

    //100 200 300 400 500
}
✨ 정답보기
 
el

11.

{
    const func = str => {
        return str;
    }
}
✨ 정답보기
 
const func = str => str;

12.

{
    const num = [100, 200, 300, 400, 500];

    for(let index of _____ ){
        document.write(index);
    }

    //결과값
    //100 200 300 400 500
}
✨ 정답보기
 
num

13.(X)

{
    function func(){
        let i = 5, j = 4, k = 1, l, m;
        l = i > 5 || j != 0;
        m = j <= 4 && k < 1;
        document.write(l);
        document.write(m);
    }
    func();
}
✨ 정답보기
 
true, false
 
 
📑 풀이
 
I변수에는 논리 연산자  ||를 사용하여 i>5 또는 j != 0 의 결과값이 할당
i변수는 5로 초기화
i>5의 결과는 false가 되고, j 변수는 4로 초기화
j != 0 의 결과는 true가 된다.
 
m변수는 논리 연산자 &&를 사용해 j <= 4 와 K < 1 의 결과값이 할당
j 변수는 4로 초기화 j <= 4의 결과는 true  k 변수는 1로 초기화
k < 1의 결과는 false가 된다.
 
✨ &&연산자 AND연산자
✨ || 연산자 OR연산자
✨ 비교 연산자 '같지 않다' 서로 다를때 true! 같으면 false
 
 
 
 

14.

{
    const arr = [100, 200, 300, 400, 500];
    const text = arr.push(600);

    document.write(arr);

    const arr2 = [100, 200, 300, 400, 500];
    const text2 = arr2.unshift(600);

    document.write(arr2);
}
✨ 정답보기
 
100,200,300,400,500,600
600,100,200,300,400,500

15.

{
    const obj = {
        a: 100, 
        b: 200
    };

    for(let key in obj) { 
        console.log(key);
    }
}
✨ 정답보기
 
a,b

16.(X)

{
    let num = 0;

    while(false){
        num++;
        if( num == 3 ){
            continue;
        }
        if( num > 6 ){
            break;
        }
    }
    console.log(num);
}
✨ 정답보기
 
0
 
📑 풀이
 
while문의 조건식이 false이기 때문에 조건이 실행되지 않는다.
 

17.

{
    let a, b, result;
    a = 7, b = 4
    result = a & b;

    console.log(result, a, b)
}
✨ 정답보기
 
4,7,4

18.

{
    let a = 1, b = 2, c = 3, result;
    result = ++a + b++ + ++c;

    console.log(result);
    console.log(a);
    console.log(b+c);
    console.log(c);
}
✨ 정답보기
 
8,2,7,4

19.(X)

{
    let data = [70, 80, 75, 60, 90];
    let best = 0;
    let score = 0;

    for(let i=0; i<data.length; i++){
        if(data[i]>80) {
            best++;
        }
        if(score < data[i]) {
            score = data[i];
        }
    }

    console.log(best, score)
}
✨ 정답보기
 
1,90
 
📑 풀이
 
배열 data에서 요소들을 순회하면서, 80보다 큰 수의 개수를 카운트 하고 가장 큰 수를 찾는 코드입니다.
for문을 사용해 data의 모든 요소를 순회하면서, 각 요소가 80보다 큰지 검사하고 큰 경우 best 변수를 증가시킵니다.
또한 score 변수보다 큰 값을 발견하면 score 변수를 해당 값으로 업데이트
 
best 변수에는 배열 data 에서 80보다 큰 수의 개수가 저장,
score 변수에는 배열 data 에서 가장 큰 수가 저장.
 
배열 data가 [70,80,75,60,90]인 경우 80보다 큰 수의 개수는 1개,
가장 큰 수는 90.
 

20.(X)

{
    function func(num1, num2){
        if(num1 > num2) return num1
        else return num2
    }
    console.log(func(10, 23) + func(40, 50))
}
✨ 정답보기
 
73
 
📑 풀이
 
 
함수를 두 번 호출한 후 그 결과를 출력하는 코드이다.
func는 두 개의 인자 num1과 num2를 받아서, num1이 num2 보다 큰 경우 num1을 반환하고, 그렇지 않은 경우num2를 반환한다.
 
그 다음 console.log 구문에서는 func(10,23)과 func(40,50)의 결과를 더한 값을 출력한다.
func(10,23) 의 결과는 23이고, func(40,50)의 결과는 50입니다. 따라서 console.log 구문에서는
23 + 50 = 73 이 출력.

 

반응형