JS/[codeit] 프로그래밍기초 in JS (완)

[ codeit ] 프로그래밍 기초 in JS - 프로그래밍과 데이터 in JS (4)

web_seul 2021. 5. 23. 12:05
반응형

 과제로 복습하기 

1강. 팩토리얼!

0! = 1

1! = 1

2! = 1 * 2 = 2

3! = 1 * 2 * 3 = 6 .. 을 구하는 함수 만들기

function factorial(n) {
  let result = 1;
	
	// 여기에 코드를 작성해 주세요.
  for(let i =1; i<=n; i++){
    result = result * i;
  }
	
  return result;
}

// 테스트 코드
console.log(factorial(12));
console.log(factorial(6));
console.log(factorial(3));
console.log(factorial(0));
function factorial(n) {
  let result = 1;

  for (let i = n; i >= 1; i--) {
    result = result * i;
  }

  return result;
}

// 테스트 코드
console.log(factorial(12));
console.log(factorial(6));
console.log(factorial(3));
console.log(factorial(0));

 

2강. 거스름돈 구하기

function calculateChange(payment, cost) {
  // 코드를 작성해 주세요.
  let rest = (payment - cost);

  
  const fiftyCount = (rest - (rest % 50000)) / 50000;
  rest = rest - 50000 * fiftyCount;
  const tenCount = (rest - (rest % 10000)) / 10000;
  rest = rest - 10000 * tenCount;
  const fiveCount = (rest - (rest % 5000)) / 5000;
  rest = rest - 5000 * fiveCount;
  const oneCount = (rest - (rest % 1000)) / 1000;
  rest = rest - 1000 * oneCount;
  
  console.log(`50000원 지폐: ${fiftyCount}장`);
  console.log(`10000원 지폐: ${tenCount}장`);
  console.log(`5000원 지폐: ${fiveCount}장`);
  console.log(`1000원 지폐: ${oneCount}장`);
}

// 테스트 코드
calculateChange(100000, 33000);
console.log('');
calculateChange(500000, 378000);

간략히

function calculateChange(payment, cost) {

  let change = payment - cost; // 거스름돈 총액

  function billCounting(amount) {
    const count = (change - (change % amount)) / amount;
    change = change - amount * count;
    return count;
  }

  const fiftyCount = billCounting(50000);
  const tenCount = billCounting(10000);
  const fiveCount = billCounting(5000);
  const oneCount = billCounting(1000);

  console.log(`50000원 지폐: ${fiftyCount}장`);
  console.log(`10000원 지폐: ${tenCount}장`);
  console.log(`5000원 지폐: ${fiveCount}장`);
  console.log(`1000원 지폐: ${oneCount}장`);
}

calculateChange(100000, 33000);
console.log('');
calculateChange(500000, 378000);

 

3강. 펠린드롬

"토마토"나 "기러기"처럼 거꾸로 읽어도 똑같은 단어를 '팰린드롬(palindrome)'

팰린드롬 여부를 확인하는 함수 isPalindrome을 작성

//내 풀이.. 오답
function isPalindrome(word) {
  // 여기에 코드를 입력해 주세요.
  for( let i = 0; i <= word.length; i++){
    if(word.slice(word[i],word[i+2]) == word.slice(word.length-1-i, word.length-3-i)){
      return true;
    }else{
      return false;
    }
  }
}

// 테스트 코드
console.log(isPalindrome("racecar"));
console.log(isPalindrome("stars"));
console.log(isPalindrome("기러기"));
console.log(isPalindrome("123321"));
console.log(isPalindrome("hello"));
console.log(isPalindrome("kayak"));
//정답풀이
function isPalindrome(word) {
  // 여기에 코드를 입력해 주세요.
  for (let i = 0; i < Math.floor(word.length / 2); i++) {
    let left = word[i];
    let right = word[word.length - 1 - i];
    if (left !== right) {
      return false;
    }
  }
  return true;
}

// 테스트 코드
console.log(isPalindrome("racecar"));
console.log(isPalindrome("stars"));
console.log(isPalindrome("기러기"));
console.log(isPalindrome("123321"));
console.log(isPalindrome("hello"));
console.log(isPalindrome("kayak"));
반응형