[알고리즘] 2024-02-13(화)

dev-riverkim·2024년 3월 13일
0

8 kyu - Even or Odd

Create a function that takes an integer as an argument and returns "Even" for even numbers or "Odd" for odd numbers.

Solution

function evenOrOdd(number) {
  return number % 2 === 0 ? "Even" : "Odd"
}
const evenOrOdd = (number) => number % 2 ? "Odd" : 'Even';

8 kyu - Transportation on vacation

After a hard quarter in the office you decide to get some rest on a vacation. So you will book a flight for you and your girlfriend and try to leave all the mess behind you.

You will need a rental car in order for you to get around in your vacation. The manager of the car rental makes you some good offers.

Every day you rent the car costs $40. If you rent the car for 7 or more days, you get $50 off your total. Alternatively, if you rent the car for 3 or more days, you get $20 off your total.

Write a code that gives out the total amount for different days(d).

Solution

function rentalCarCost(d) {
  // Your solution here
  //하루 렌트비는 $40입니다.
  //7일 이상 렌터카를 대여하면 총 금액에서 $50 할인을 받을 수 있습니다.
  //또는 3일 이상 렌터카를 렌트하면 총 금액에서 $20 할인을 받을 수 있습니다.

  //다른 날짜(d)에 대한 총 금액을 제공하는 코드를 작성하세요.

  if(d >= 7){
   return (d * 40) - 50;
  } else if(d >= 3){
  return (d * 40) - 20;
  } else {
    return d * 40
  }

}
function baseCost(days, rate) {
  return days * rate;
}

function discountRate(days) {
  if ( days >= 7 ) {
    return 50;
  }
  else if ( days >= 3 ) {
    return 20;
  }
  else {
    return 0;
  }
}

function rentalCarCost(days) {
  return baseCost(days, 40) - discountRate(days);
}
const rentalCarCost = d => d * 40 - ((d > 6) ? 50 : ((d > 2) ? 20 : 0));
function rentalCarCost(d) {
  return d * 40 - (d >= 7 ? 50 : (d >= 3 ? 20 : 0));
}
function rentalCarCost(d) {
  if(d<3) return d * 40;
  if(7>d && d>=3) return (d*40 -20);
  if(d>=7) return (d*40 -50);
}
function rentalCarCost(days) {
    var dayCost = 40;
    
    var discount = 0;
    if(days >= 3) discount += 20;
    if(days >= 7) discount += 30;
    
    return dayCost * days - discount;
}

8 kyu - L1: Set Alarm

Write a function named setAlarm/set_alarm/set-alarm/setalarm (depending on language) which receives two parameters. The first parameter, employed, is true whenever you are employed and the second parameter, vacation is true whenever you are on vacation.

The function should return true if you are employed and not on
vacation (because these are the circumstances under which you need to
set an alarm). It should return false otherwise. Examples:

employed | vacation
true     | true     => false
true     | false    => true
false    | true     => false
false    | false    => false

Solution

function setAlarm(employed, vacation){
  return employed === true && vacation === false ? true : false
}
const setAlarm = (employed, vacation) => employed && !vacation;
function setAlarm(employed, vacation){
  return employed && !vacation;
}
function setAlarm(employed, vacation){
  return (employed && !vacation) ? true : false;
}
profile
dev-riverkim

0개의 댓글