[자바스크립트] 계산기 연산 후 숫자 수정, if문 중첩 줄이기

휘루·2023년 7월 1일
0

자바스크립트

목록 보기
35/40

계산기 숫자 수정

현재 3 + 4 누르면 34로 출력되는 현상이 있습니다.
그래서 이걸 고쳐야 하는데요.

const onClickNumber = (event) => {
// operator는 사칙연산입니다.
if (operator) { // 사칙연산 시작
	if (numTwo) { // 사칙연산 누르면 첫번째 숫자가 사라져야겠죠, 그래서 numTwo가 아닌 경우는 !numTwo입니다.
    	$result.value = ''; // 결과를 value(값)을 비웁니다 '';
    };
    numTwo = numTwo + event.target.textContent; // 두번째 숫자의 컨텍스트를 합하고
} else {
	numOne = numOne + event.target.textContent; // 첫번째 숫자의 컨텍스트를 합합니다.
  };
  $result.value = $result.value + event.target.textContent; // 나머지 결과value(값)을 컨텍스트와 더합니다
};

이런 식으로 코드를 수정합니다. if문 안에 if문을 넣었는데요.
이걸 더 줄이는 방법이 있습니다.

if 중첩문 줄이기

위의 코드를 if중첩문을 더 줄여보면

const onClickNumber = (event) => {
	if(!operator) { // 비어있다, operator(사칙연산)이 아닌 경우
		numOne += event.target.textContent;
		$result.value += event.target.textContent;
		return;
	};
	if(numTwo) {// 비어있지 않다
		$result.value = '';
	};
		numTwo += event.target.textContent;
		$result.value += event.target.textContent;
	};

이렇게 줄여질 수 있습니다.

숙제 1

if (조건 A) {
	if (조건 B) {
    	if (조건 C) {
        
        };
    } else {
    	if (조건 D) {
        	if (조건 E) {
            	// 난 언제 실행?
            } else {
            	// 나는 언제?
            };
        } else {
        	// 나는 언제?
        };
    };
};

이걸 줄여보세요. 숙제입니다.

숙제 2

function test() {
	let result = '';
    if (a) {
    	if (!b) {
        	result = 'c';
        };
    } else {
    	result = 'a';
    };
    result += 'b';
    return result;
};

줄여보세요.

function test() {
	let result = '';
    if (a) {
    	if (!b) {
        	result = 'c';
        };
    } else {
    	result = 'a';
    };
    result += 'b';
    return result;
};
  • 일단 if문 다음에 나오는 중복된 절차를 찾아야 합니다.
  • result += 'b';
    return result; 가 중복된 절차 내용입니다.
  • 위의 내용을 if문 안쪽에 넣어줍니다.
function test() {
	let result = '';
    if (a) {
    	if (!b) {
        	result = 'c';
        };
        result += 'b';
    	return result;
    } else {
    	result = 'a';
        result += 'b';
    	return result;
    };
};
  • 이제 짧은 단계를 위로 올립니다.
  • !b랑 else중에 짧은건 else입니다. else를 위로 올립니다.
  • 그럼 else는 a가 아닐때 실행하니 a를 !a로 바꿉니다.
  • 이제 else 내용을 !a에 넣습니다.
function test() {
	let result = '';
    if (!a) {
    	result = 'a';
        result += 'b';
    	return result;
    	if (!b) {
        	result = 'c';
        };
        result += 'b';
    	return result;
    } else {
    	
    };
};
  • 이제 !a 안에 'a', 'b'와 result가 안에 들어가집니다.
  • if (!b) 내용을 잘라서 else 안에 넣어줍니다.
function test() {
	let result = '';
    if (!a) {
    	result = 'a';
        result += 'b';
    	return result;
    } else {
    	if (!b) {
        	result = 'c';
        };
        result += 'b';
    	return result;
    };
};
  • if (!b) 안에 있는 내용은 else로 빼줍니다.
  • 짧은애를 위로 올리고 짧은애가 끝났으면 return을 붙여주는데 return이 이미 있으므로 마칩니다.
  • 보통은 여기서 끝인데 상단에 return이 나오면 else가 필요 없어집니다. else를 지웁니다.
function test() {
	let result = '';
    if (!a) {
    	result = 'a';
        result += 'b';
    	return result;
    } 
    if (!b) {
    	result = 'c';
    };
    result += 'b';
    return result;
};

결과값입니다.
if문 안에 if문이 있던게 간결하게 줄었습니다.

profile
반가워요

0개의 댓글