TEST&복습

javascript test 복습하기

hyejeong3283 2023. 3. 4. 18:39
728x90
반응형

1. 1-50까지 for문으로 출력

for(let i=1; i<=50; i++){
    document.write(i);
}

2. 1-50까지 while문으로 출력

let num=1;
while(num<=50){
    document.write(num);
    num++;
}


3. 1-100까지 짝수로 출력(if문 사용X)

for(let i=2; i<=100; i+=2){
    document.write(i);
}


4. 1-100까지 홀수로 출력(if문 사용)

for(let i=1; i<=100; i++){
    if(i%2==1){
    	document.write(i);
    }
}

5. 테이블 50칸 출력(1-50숫자 출력)

let table = "<table border='1'>";
num1 = 1;
for(let i=1; i<=10; i++){
    table += "<tr>";
    for(let j=1; j<=5; j++){
        table += "<td>"+num1+"</td>";
        num1++;
    }
    table += "</tr>";
}
table += "</table>";
document.write(table);


6. 1-10까지 합 출력

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

 


7. 구구단 출력

 for(let i=2; i<=9; i++){
    document.write(i+"단"+"<br>");
    for(let j=1; j<=9; j++){
        document.write(i+"*"+j+"="+i*j+"<br>");
    }
}


8. 함수 유형 4가지 작성

// 선언적 함수
function func(){
    document.write("실행되었습니다.");
}
func();

// 익명 함수
const func = function(){
	document.write("실행되었습니다.");
}
func();

// 매개변수 함수
function func (str){
	document.write(str);
}
func("실행되었습니다.");

// 리턴값 함수
function func(){
	const str = "실행되었습니다.";
    return str;
}
document.write(func());

9. 화살표 함수유형 4가지 작성

// 선언적 함수 (화살표)
func = () => {
    document.write("실행되었습니다.");
}
func();

// 익명 함수 (화살표)
const func = () => {
	document.write("실행되었습니다.");
}
func();

// 매개변수 함수 (화살표)
func = (str) => {
	document.write(str);
}
func("실행되었습니다.");

// 리턴값 함수 (화살표)
func = () => {
	const str = "실행되었습니다.";
    return str;
}
document.write(func());