유데미 - Logical Operators & The switch Statement

MK·2022년 5월 11일

Udemy 강의!

목록 보기
8/12

논리 연산이라고

이런 간단한 표입니다. 큰 내용은 없으니 사진으로 대체하고 넘어가도록 하겠습니다.

😍과제

let country = "South Korea";
let population = 47;
let isIsland = false;
let language = "english";

if (language === `english` && population < 50 && isIsland === false) {
  console.log(`You should live in ${country}`);
} else {
  console.log(`South Korea  does not meet your criteria :(`);
}

아직도 ${ }이 구문을 쓰는게 익숙하지가 않아서 안썼다가 답을 보고 추가했습니다.
갈길이 멀지만 그래도 낫배드!


switch문을 알아보도록 하겠습니다.
if else문과 비슷하지만
다릅니다.

요즘은 많이 안쓰는 구문이긴 하지만 알아둔다면 언젠가는 쓰게 되겠죠?

const day = "wednesday";

switch (day) {
  case "monday": //day === 'monday'
    console.log("Plan course structure");
    console.log("Go to coding meetup");
    break;
  case "tuesday":
    console.log(`Prepare theory videos`);
    break;
  case "wednesday":
  case "thursday":
    console.log(`Write code examples`);
    break;
  case "friday":
    console.log(`Record videos`);
    break;
  case "saturday":
  case "sunday":
    console.log("Enjoy the weekend 😀");
    break;
  default:
    console.log("Not a valid day!");
}

정말 간단합니다. switch case 그리고 조건이 끝났다는 의미에서 break만 써주면 완성이 됩니다!
수요일과 목요일사이에 break가 없으니 수요일을 입력하니 목요일과 같은 일과가 나오는것을 확인할 수 있습니다!

이렇게 if else보다 가시성이 좋은걸 출력하게 됩니다.

만약 이걸 if else로 만들려면 어떻게 하면 될까요?

const day = "saturday";

if (day === "monday") {
  console.log("Plan course structure");
  console.log("Go to coding meetup");
} else if (day === "tuesday") {
  console.log(`Prepare theory videos`);
} else if (day === "wednesday" || day === "thursday") {
  console.log(`Write code examples`);
} else if (day === "friday") {
  console.log(`Record videos`);
} else if (day === "saturday" || day === "sunday") {
  console.log("Enjoy the weekend 😀");
}

이렇게 만들어 봤는데요.
수요일과 목요일에서는
(day === "wednesday" || "thursday")
이렇게 입력해도 원하는 값(Write code examples)이 출력이 되었는데,
토,일에도 (Write code examples)이 출력 되었습니다.

그래서 강의를 보니 목요일과 일요일에도 각각 똑같다고 입력을 해줘야하더라고요.

크게 어려운건 없으니 이만!

😍과제

const language = "arabic";

switch (language) {
  case "chinese":
  case "mandarin":
    console.log("MOST number of native speakers!");
    break;
  case `spanish`:
    console.log("2nd place in number of native speakers");
    break;
  case `english`:
    console.log("3rd place");
    break;
  case `hindi`:
    console.log("Number 4");
    break;
  case `arabic`:
    console.log("5th most spoken language");
    break;
  default:
    console.log("Great language too :D");
    break;
}
profile
그라타타타

0개의 댓글