본문 바로가기

Python_WEB/JavaScript

[freecodecamp]JavaScript if 문과 함께 논리연산자 사용

반응형

If 문은 코드에서 결정을 내리는 데 사용됩니다.

키워드 if는 괄호 안에 정의 된 특정 조건에서 중괄호로 코드를 실행하도록 JavaScript에 지시합니다.

이러한 조건을 부울 조건이라고하며 true 또는 false 만 가능합니다.

조건이 참으로 평가되면 프로그램은 중괄호 안에있는 명령문을 실행합니다.

부울 조건이 false로 평가되면 중괄호 안의 문이 실행되지 않습니다.

 

if (condition is true) {
  statement is executed
}

 

예>

function test (myCondition) {
  if (myCondition) {
     return "It was true";
  }
  return "It was false";
}
test(true);  // returns "It was true"
test(false); // returns "It was false"

 

true 값으로 test가 호출되면 if 문은 myCondition을 평가하여 true인지 아닌지 확인합니다.

true이므로 함수는 "It was true"를 반환합니다.

false 값으로 test를 호출하면 myCondition은 true가 아니고 중괄호 안의 문이 실행되지 않고 함수는 "It was false"를 반환합니다.

 

Q>

1. 매개 변수가 wasThatTrue이면 "Yes, that was true"를 리턴하고 그렇지 않으면 "No, that was false"를 리턴하는 함수 내부에 if 문을 작성하십시오.

2. trueOrFalse는 함수 여야합니다.

3. trueOrFalse (true)는 문자열을 반환해야합니다.

4. trueOrFalse (false)는 문자열을 반환해야합니다.

5. trueOrFalse (true)는 "Yes, that was true"를 반환해야합니다.

6. trueOrFalse (false)는 "No, that was false"를 반환해야합니다.

 

A>

function trueOrFalse(wasThatTrue) {
  // Only change code below this line
  if (wasThatTrue) {
    return "Yes, that was true";
  }
  return "No, that was false";

  // Only change code above this line
}

console.log(trueOrFalse(true));
console.log(trueOrFalse(false));

 

https://www.freecodecamp.org/

 

freeCodeCamp.org

Learn to code. Build projects. Earn certifications.Since 2015, 40,000 graduates have gotten jobs at tech companies including Google, Apple, Amazon, and Microsoft.

www.freecodecamp.org

 

반응형