Sol )
function add () { let sum; sum = 7 + 8; return sum; } add(); // 함수 호출 // console.log(add()); 사용시 콘솔창에 결과 출력됨. 15
Sol )
function addFive(number) { return 5 + number; } addFive(3); // console.log(addFive()); 사용시 콘솔창에 결과 출력됨. 8
Sol )
function divideByTwo(number) { return number / 2; } divideByTwo(10); // console.log(divideByTwo()); 사용시 콘솔창에 결과 출력됨. 5
Sol )
function increment() { let myVar = 83; myVar++ return myVar; } increment(); // console.log(increment()) 사용시 콘솔창에 결과 출력됨. 84
Sol )
function decrement() { let num1 = 11; let num2 = 44; num1++ num2-- if(num1 === 12 && num2 === 43) { return "Pass"; } else { return "Try again"; } } decrement(); // console.log(decrement()) 사용시 콘솔창에 결과 출력됨. Pass
자바스크립트 산술연산자에는 기본적인 덧셈(+), 뺄셈(-), 곱셈(*), 나눗셈(/) 외에도 % 연산자가 있다. %는 왼쪽의 숫자를 오른쪽 숫자로 나눠서 나머지를 구하는 연산자이다.
5 % 2 // --> 1 (정답)
48 % 2 // --> 0 (오답)
findRemainder(); // --> 1 (정답)
Sol )
function findRemainder() { let remainder; remainder = 21 % 2; return remainder; } findRemainder(); // --> 1
Sol )
function oddOrEven(num) { if (num % 2 == 0) { // num을 2로 나눴을 때 나머지가 0 = 짝수 return 'Even'; } else { return 'Odd'; } } oddOrEven(20); // --> "Even"