if~else
else 문은 조건식을 만족할(true) 경우와 만족하지 않을(false) 경우에 따라 실행되는 코드가 달라집니다.
Last updated
Was this helpful?
else 문은 조건식을 만족할(true) 경우와 만족하지 않을(false) 경우에 따라 실행되는 코드가 달라집니다.
Last updated
Was this helpful?
Was this helpful?
if(조건식) { //참(true)일 때 실행 코드 } else { //거짓(false)일 때 실행 코드 }
//숫자 크기
let num = prompt("당신이 좋아하는 숫자는?");
if (num > 100) {
document.write("숫자는 100보다 큽니다.");
} else {
document.write("숫자는 100보다 작습니다.");
}
//짝수 홀수 구하기
let num2 = prompt("당신이 좋아하는 숫자는?");
if (num2 % 2 == 0) {
document.write("짝수입니다.");
alert("짝수임.");
} else {
document.write("홀수입니다.");
alert("홀수임.");
}
//아이디 비밀번호 구분하기
let userID = prompt("아이디를 입력하세여.");
let userPW = prompt("비밀번호를 입력하세요.");
if( userID == "pasahu13" && userPW == "1234"){
alert("환영합니다.");
document.write("환영합니다.");
} else {
alert("아이디 또는 비밀번호를 확인해주세요!");
document.write("아이디 또는 비밀번호를 확인해주세요!.");
}
//확인하기
let conf = confirm("정말로 탈퇴할거냐??");
if(conf){
document.write("잘가라~나를 잊어라~.");
alert("잘가라~나를 잊어라~.");
} else {
document.write("탈퇴처리가 취소되었습니다.");
alert("탈퇴처리가 취소되었습니다.");
}