개발공부/자바스크립트

[개념] 조건문, 반복문

jennayeo 2021. 6. 11. 12:00

조건문

if (조건) {조건을 만족할 때 실행 할 코드}

if (props.board_name === "참석한 모임") {
      swal("후기글 작성은 후기글 게시판에서 작성 가능합니다! 이동하시겠어요?",{      
          buttons : {
            cancel : "🤔 여기 있을래요!",
            ok : "😆 이동할래요!"
          }
      })

 

else

const shoesPrice = 30000
if (shoesPrice < 40000) {
	console.log('이 신발을 사겠습니다.")
} else {
	console.log('이 신발을 사지않겠습니다.")
}
const clickScrollButton = () => {
    if (isMiddle===true) {
      window.scrollTo({ top: 900, left: 0, behavior: "smooth" });
      if (isSmall===true) {
        window.scrollTo({ top: 770, left: 0, behavior: "smooth" });      
      }
    }else{
      window.scrollTo({ top: 1150, left: 0, behavior: "smooth" });
    }    
  };

else if

const shoesPrice = 50000
if (shoesPrice < 400000) {
	console.log('이 신발을 사겠습니다.')
} else if (shoesPrice <= 50000) {
	console.log('고민을 해볼게요.')
} else {
	console.log('너무 비싸요!')
}

 

반복문

while (조건) {조건을 만족할 때 실행할 코드}

let temperature = 20
while (temperature < 25) {
	console.log(`${temperature}도 적당한 온도입니다.`)
   temperature++ //증감연산자로 온도 변화
}

for (begin; conditon; step) {조건을 만족할 때 실행할 코드}

for (let temperature = 20; temperature < 25; temperature++) {
	console.log(`${temperature}도 정도면 적당한 온도입니다.`)
}

 

조건문, 반복문 활용

for (let number = 1; number <= 10; number++) {
	if (number % 3 === 0) {
    	console.log(`${number}는 3으로 나눠서 떨어지는 숫자입니다.`)
    }
}