[Practice 4] 1 ~ 40 Solutions

Strawberry Oolong Teaยท2021๋…„ 9์›” 22์ผ
0

TODAY I LEARNED

๋ชฉ๋ก ๋ณด๊ธฐ
30/51
post-thumbnail

๋ณ€์ˆ˜์™€ ์ž๋ฃŒํ˜•

๐Ÿฆ„ Variables

1. ๋ณ€์ˆ˜ unicorn์„ ์„ ์–ธํ•˜๊ธฐ

let ํ‚ค์›Œ๋“œ๋ฅผ ์‚ฌ์šฉํ•ด ๋ณ€์ˆ˜ unicorn ์„ ์–ธํ•˜๊ธฐ

let unicorn;

2. ๋ณ€์ˆ˜ unicorn์— ๋ฌธ์ž์—ด ํ• ๋‹นํ•˜๊ธฐ

์„ ์–ธ๋œ ๋ณ€์ˆ˜ unicorn ์— ๋ฌธ์ž์—ด ํ• ๋‹นํ•˜๊ธฐ

let unicorn;
unicorn = 'unicorn is real';

๐Ÿ’ฌ Expressions

ํ‘œํ˜„์‹(Expression)์€ ํ•˜๋‚˜์˜ ๊ฐ’์œผ๋กœ ํ‘œํ˜„๋˜๋Š” ์ฝ”๋“œ๋ฅผ ์˜๋ฏธํ•œ๋‹ค.

3. ๊ฑฐ์†์‹œ(๊ฑฐ๋ฆฌ, ์†๋ ฅ, ์‹œ๊ฐ„)

  • ๊ฑฐ๋ฆฌ = ์†๋ ฅ * ์‹œ๊ฐ„
  • ์†๋„ = ๊ฑฐ๋ฆฌ / ์‹œ๊ฐ„
  • ์‹œ๊ฐ„ = ๊ฑฐ๋ฆฌ / ์†๋„
let distance, speed, time;

๊ฑฐ๋ฆฌ

speed = 5; // km
time = 2; // hour
distance = speed * time; // 10km

์†๋„

distance = 10; // km
time = 2; // hour
speed = distance / time; // 5km

์‹œ๊ฐ„

distance = 10; // km
speed = 5; // km
time = distance / speed; // 2hour

๐Ÿ•น Functions

๋ฌธ์ œ๋ฅผ ์–ด๋–ป๊ฒŒ ํ•ด๊ฒฐํ•  ์ง€์— ๋Œ€ํ•œ ๊ณ ๋ฏผ์„ ์•Œ๊ณ ๋ฆฌ์ฆ˜์ด๋ผ๊ณ  ํ•˜๋ฉฐ
ํ•จ์ˆ˜๋Š” ์•Œ๊ณ ๋ฆฌ์ฆ˜์„ ์ฒด๊ณ„์ ์œผ๋กœ ๊ตฌ์„ฑํ•œ ํ•˜๋‚˜์˜ ์ž‘์€ ๊ธฐ๋Šฅ์ด๋ผ๊ณ  ๋ณผ ์ˆ˜ ์žˆ๋‹ค.

4. ์น˜ํƒ€์˜ ์†๋„

์น˜ํƒ€์˜ ์†๋„๋Š” 80 ~ 130km/h.
์ฃผ์–ด์ง€๋Š” ๊ฑฐ๋ฆฌ๋ฅผ ๋ช‡ ์‹œ๊ฐ„๋งŒ์— ๋‹ฌ๋ฆฌ๋Š”์ง€๋ฅผ ๊ตฌํ•˜๋Š” ํ•จ์ˆ˜ ๊ตฌํ˜„ํ•˜๊ธฐ

function getRunCheetahTime(distance, speed) {
  let time;
  time = distance / speed;
  return time;
}

5. ํ•จ์ˆ˜ ์„ ์–ธ์‹๊ณผ ํ•จ์ˆ˜ ํ‘œํ˜„์‹

ํ•จ์ˆ˜ ์„ ์–ธ์‹

function plusFive(number) {
  number = number + 5;
  return nunber;
}

ํ•จ์ˆ˜ ํ‘œํ˜„์‹

let minusFive(number) {
  number = number - 5;
  return number;
};

6. ํ•จ์ˆ˜์˜ ํ˜ธ์ถœ (invoke; call)

์œ ๋‹ˆ์ฝ˜์˜ ๋ฟ”

๋ฌธ์ž์—ด์„ ์ธ์ž๋กœ ๋ฐ›์•„ ๋ฌธ์ž์—ด ๋์— ์œ ๋‹ˆ์ฝ˜์„ ๋ถ™์—ฌ์„œ ๋ฆฌํ„ดํ•˜๋Š” ํ•จ์ˆ˜
attachUnicorn ์ด ์žˆ๋‹ค.

function attachUnicorn(word) {
  if (typeof word !== 'string) {
    return 'wrong type'
  } else {
    return word + '๐Ÿฆ„';
  }
}

ํ•จ์ˆ˜ attachUnicorn ์˜ ์ธ์ž๋กœ ๋ฌธ์ž์—ด 'unicorn is real' ์„ ๋„ฃ์–ด์„œ ํ˜ธ์ถœํ•˜๊ณ 
๊ทธ ๊ฒฐ๊ณผ๊ฐ’์„ ๋ณ€์ˆ˜ unicorn ์— ํ• ๋‹นํ•˜๊ธฐ

let unicorn = attachUnicorn('unicorn is real');

๐Ÿ“ Data Type (๋ฌธ์ž์—ด๊ณผ ์ˆซ์ž)

7. ๋™๋ฌผ์›์— ์žˆ๋Š” ๋™๋ฌผ์˜ ์ˆ˜

๋™๋ฌผ์ด ๋ช‡ ๋งˆ๋ฆฌ ์žˆ๋Š”์ง€ ์•Œ๋ ค์ฃผ๋Š” ๋ฌธ์ž์—ด์„ ๋ฆฌํ„ดํ•˜๋Š” ํ•จ์ˆ˜ ๊ตฌํ˜„ํ•˜๊ธฐ

let animalName, numberOfAnimal;
animalName = 'Unicorn';
numberOfAnimal = 7;

function zoo(animalName, numberOfAnimal) {
  if (numberOfAnimal === 1) {
    return 'There is ' + numberOfAnimal + ' ' + animalName + '.';
  }
  return 'There are ' + numberOfAnimal + ' ' + animalName + "s.";
}
let result = zoo(animalName, numberOfAnimal);

8. (String + Number) vs (Number + Number)

String ํƒ€์ž…๊ณผ Number ํƒ€์ž…์„ ๋”ํ•˜๋ฉด String ํƒ€์ž…์œผ๋กœ ๋ณ€ํ™˜ํ•œ๋‹ค.

function getYear(currentYear, plusYear) {
  return currentYear + plusYear;
}

getYear(2021, 1) // 2022
getYear('2021', 1) // '20211'

9. Number() ํ•จ์ˆ˜

  • ์ธ์ˆ˜๋ฅผ ์ˆซ์ž๋กœ ๋ณ€ํ™˜ํ•  ์ˆ˜ ์—†์œผ๋ฉด NaN์„ ๋ฆฌํ„ด
  • ๋นˆ ๋ฌธ์ž์—ด ''๊ณผ null์€ 0์„ ๋ฆฌํ„ด

์ž„์˜์˜ ๊ฐ’์„ ์ž…๋ ฅ๋ฐ›์•„ Number ํƒ€์ž…์„ ๋ฆฌํ„ดํ•˜๋Š” ํ•จ์ˆ˜ ๊ตฌํ˜„ํ•˜๊ธฐ

function convertToNumber(something) {
  return Number(something)
}

10. String() ํ•จ์ˆ˜

์ž„์˜์˜ ๊ฐ’์„ ์ž…๋ ฅ๋ฐ›์•„ String ํƒ€์ž…์„ ๋ฆฌํ„ดํ•˜๋Š” ํ•จ์ˆ˜ ๊ตฌํ˜„ํ•˜๊ธฐ

function converToString(something) {
  return String(something)
}

์กฐ๊ฑด๋ฌธ

โš–๏ธ if else ์กฐ๊ฑด๋ฌธ

11. ์ฒญ์†Œ๋…„๊ด€๋žŒ๋ถˆ๊ฐ€

๋งŒ 18์„ธ ๋ฏธ๋งŒ์˜ ์ž๋Š” ๊ด€๋žŒํ•  ์ˆ˜ ์—†๋Š” ์˜์ƒ๋ฌผ๋กœ,
๋“ฑ๊ธ‰๋ถ„๋ฅ˜ ๊ธฐ์ค€์ด ๋˜๋Š” 7๊ฐ€์ง€ ๊ณ ๋ ค์š”์†Œ๊ฐ€
๊ตฌ์ฒด์ , ์ง์ ‘์ , ๋…ธ๊ณจ์ ์œผ๋กœ ํ‘œํ˜„๋œ ์ž‘ํ’ˆ์„ ๋งํ•œ๋‹ค.

  • ์ฃผ์ œ์˜ ๊ธฐ์ค€
  • ์„ ์ •์„ฑ์˜ ๊ธฐ์ค€
  • ํญ๋ ฅ์„ฑ์˜ ๊ธฐ์ค€
  • ๋Œ€์‚ฌ์˜ ๊ธฐ์ค€
  • ๊ณตํฌ์˜ ๊ธฐ์ค€
  • ์•ฝ๋ฌผ์˜ ๊ธฐ์ค€
  • ๋ชจ๋ฐฉ์œ„ํ—˜์˜ ๊ธฐ์ค€

์ฒญ์†Œ๋…„๊ด€๋žŒ๋ถˆ๊ฐ€ ์˜ํ™”๋ฅผ ๋ณผ ์ˆ˜ ์žˆ๋Š”์ง€ ์—†๋Š”์ง€ ๋ฆฌํ„ดํ•˜๋Š” ํ•จ์ˆ˜ ๊ตฌํ˜„ํ•˜๊ธฐ

function isOldEnoughToSeeMovie(age) {
  if (age >= 18) {
    return true;
  } else {
    return false;
  }
}

12. ์ ์‹ฌ๊ฐ’

์Œ์‹๊ฐ’์„ ์ž…๋ ฅ๋ฐ›์•„ ์ ์‹ฌ๊ฐ’ ์กฐ๊ฑด์— ๋”ฐ๋ฅธ ๊ฒฐ๊ณผ๋ฅผ ๋ฆฌํ„ดํ•˜๋Š” ํ•จ์ˆ˜ ๊ตฌํ˜„ํ•˜๊ธฐ

  • ํ•˜๋ฃจ ์ ์‹ฌ ์‹๋น„์ธ 12,000์›์„ ์ดˆ๊ณผํ•˜๋ฉด '๋„ˆ๋ฌด ๋น„์‹ธ์š” ๐Ÿ˜ญ'
  • ํ•˜๋ฃจ ์ ์‹ฌ ์‹๋น„์ธ 12,000์› ์ดํ•˜์ด๋ฉด '๋ง›์ ํ•˜์„ธ์š”! ๐Ÿ™Œ'
function isExpensiveThan12000(price) {
  if (price > 12000) {
    return '๋„ˆ๋ฌด ๋น„์‹ธ์š” ๐Ÿ˜ญ';
  } else {
    return '๋ง›์ ํ•˜์„ธ์š”! ๐Ÿ™Œ';
  }
}

13. 100์  ๋งŒ์ ์— 100์ 

์ ์ˆ˜๊ฐ€ 100์ ์ธ์ง€ ์•„๋‹Œ์ง€๋ฅผ ๋ฆฌํ„ดํ•˜๋Š” ํ•จ์ˆ˜ ๊ตฌํ˜„ํ•˜๊ธฐ

function equals100(score) {
  return score === 100;
}

14. ํ‚ค ์žฌ๊ธฐ

์ฒซ ๋ฒˆ์งธ ์‚ฌ๋žŒ๋ณด๋‹ค ๋‘ ๋ฒˆ์งธ ์‚ฌ๋žŒ์ด ํ‚ค๊ฐ€ ๋” ํฐ์ง€๋ฅผ ๋ฆฌํ„ดํ•˜๋Š” ํ•จ์ˆ˜ ๊ตฌํ˜„ํ•˜๊ธฐ

function isTallerThan(prev, next) {
  if (prev < next) {
    return true;
  }
  return false;
}

15. 3์˜ ๋ฐฐ์ˆ˜

3์˜ ๋ฐฐ์ˆ˜์ด๋ฉด '๐Ÿ‘' ๋ฅผ ๋ฆฌํ„ดํ•˜๊ณ  3์˜ ๋ฐฐ์ˆ˜๊ฐ€ ์•„๋‹ˆ๋ฉด '๐Ÿ’ฉ' ์„ ๋ฆฌํ„ดํ•˜๋Š” ํ•จ์ˆ˜ ๊ตฌํ˜„ํ•˜๊ธฐ

function isTriple(num) {
  if (num % 3 === 0) {
    return '๐Ÿ‘';
  }
  return '๐Ÿ’ฉ';
}

16. 7๋ณด๋‹ค ํฌ๊ณ  ํ™€์ˆ˜์ธ ์ˆ˜

7๋ณด๋‹ค ํฌ๊ณ  ํ™€์ˆ˜์ธ์ง€ ์—ฌ๋ถ€๋ฅผ ๋ฆฌํ„ดํ•˜๋Š” ํ•จ์ˆ˜ ๊ตฌํ˜„ํ•˜๊ธฐ

function isOddAndGreaterThanSeven(num) {
  if (num > 7 && num % 2 !== 0) {
    return true;
  }
  return false;
}

17. ํ–„๋ฒ„๊ฑฐ ์„ธํŠธ ๋งŒ๋“ค๊ธฐ

5๋กœ ๋‚˜๋ˆ„์–ด ๋–จ์–ด์ง€๋ฉด '๐Ÿ”'
7๋กœ ๋‚˜๋ˆ„์–ด ๋–จ์–ด์ง€๋ฉด '๐ŸŸ'
5์™€ 7๋กœ ๋ชจ๋‘ ๋‚˜๋ˆ„์–ด ๋–จ์–ด์ง€๋ฉด '๐Ÿฅค'
5์™€ 7๋กœ ๋ชจ๋‘ ๋‚˜๋ˆ„์–ด ๋–จ์–ด์ง€์ง€ ์•Š์œผ๋ฉด '๐Ÿ’ฉ' ๋ฆฌํ„ดํ•˜๋Š” ํ•จ์ˆ˜ ๊ตฌํ˜„ํ•˜๊ธฐ

function makeBurgerSet(num) {
  if (num % 5 === 0 && num % 7 === 0) {
    return '๐Ÿ”๐ŸŸ๐Ÿฅค';
  } else if (num % 5 === 0) {
    return '๐Ÿ”';
  } else if (num % 7 === 0) {
    return '๐ŸŸ';
  } else {
    return '๐Ÿ’ฉ';
  }
}

18. ๋ฏธ๋‹ˆ ๊ณ„์‚ฐ๊ธฐ

๋‘ ์ˆ˜๋ฅผ ์ž…๋ ฅ๋ฐ›์€ ๊ธฐํ˜ธ๋กœ ๊ณ„์‚ฐํ•˜์—ฌ ๋ฆฌํ„ดํ•˜๊ณ 
'+', '-', '*', '/' ๊ฐ€ ์•„๋‹Œ ๊ฒฝ์šฐ
'๐Ÿ’ฃ' ๋ฅผ ๋ฆฌํ„ดํ•˜๋Š” ํ•จ์ˆ˜ ๊ตฌํ˜„ํ•˜๊ธฐ

function microCalculator(operand1, operand2, operator) {
  if (operator === '+') {
    return operand1 + operand2;
  } else if (operator === '-') {
    return operand1 - operand2;
  } else if (operator === '*') {
    return operand1 * operand2;
  } else if (operator === '/') {
    return operand1 / operand2;
  } else {
    return '๐Ÿ’ฃ';
  }
}

19. ๋‹ฌ์˜ ๋งˆ์ง€๋ง‰ ๋‚ 

์—ฐ๋„์™€ ๋‹ฌ์„ ์ž…๋ ฅ๋ฐ›์•„ ๋งˆ์ง€๋ง‰ ๋‚ ์ด ๋ฉฐ์น ์ธ์ง€ ๋ฆฌํ„ดํ•˜๋Š” ํ•จ์ˆ˜ ๊ตฌํ˜„ํ•˜๊ธฐ

function lastDayInMonth(year, month) {
  const today = new Date();
  const lastDayInMonth = new Date(year, month, 0);
  return lastDayInMonth.getDate();
}

20. 13์›”์˜ ๋ˆˆ๊ฝƒ

๋‹ฌ์„ ์ž…๋ ฅ๋ฐ›์œผ๋ฉด ๋‹ค์Œ ๋‹ฌ์„ ๋ฆฌํ„ดํ•˜๋Š” ํ•จ์ˆ˜ ๊ตฌํ˜„ํ•˜๊ธฐ
1์›” โžก๏ธ 2์›”, 12์›” โžก๏ธ 'โ„๏ธ', 13์›” โžก๏ธ 1์›”

function undecimber(month) {
 if (month === 1) {
  return '2์›”';
 } else if (month === 2) {
   return '3์›”';
 } else if (month === 3) {
   return '4์›”';
 } else if (month === 4) {
   return '5์›”';
 } else if (month === 5) {
   return '6์›”';
 } else if (month === 6) {
   return '7์›”';
 } else if (month === 7) {
   return '8์›”';
 } else if (month === 8) {
   return '9์›”';
 } else if (month === 9) {
   return '10์›”';
 } else if (month === 10) {
   return '11์›”';
 } else if (month === 11) {
   return '12์›”';
 } else if (month === 12) {
   return 'โ„๏ธ';
 } else if (month === 13) {
   return '1์›”';
 } else {
   return '๐Ÿ’ฉ';
 }
}

21. 2๊ฐ€์ง€๋ฅผ ๋ชจ๋‘ ๋งŒ์กฑํ•˜๋Š” ์ˆ˜

๋‘ ์ˆ˜๋ฅผ ์ž…๋ ฅ๋ฐ›์•„ ์ ์–ด๋„ ํ•˜๋‚˜์˜ ์ˆ˜๋Š” ์ง์ˆ˜์ด๊ณ  ๋‘ ์ˆ˜ ๋ชจ๋‘ 50๋ณด๋‹ค ํฐ ์ˆ˜์ผ ๊ฒฝ์šฐ
true๋ฅผ ๋ฆฌํ„ดํ•˜๊ณ  ์•„๋‹Œ ๊ฒฝ์šฐ false๋ฅผ ๋ฆฌํ„ดํ•˜๋Š” ํ•จ์ˆ˜ ๊ตฌํ˜„ํ•˜๊ธฐ

function isEitherEvenAndMoreThan50(num1, num2) {
  if (num1 > 50 && num2 > 50) {
    if (num1 % 2 === 0 || num2 % 2 === 0) {
      return true;
    }
    return false;
  }
  return false;
}

22. ๋ฏธ์„ธ๋จผ์ง€ ์ธก์ •๊ธฐ

์ˆ˜๋ฅผ ์ž…๋ ฅ๋ฐ›์•„ ๋ฏธ์„ธ๋จผ์ง€ ๋†๋„๋ฅผ ๋ฆฌํ„ดํ•˜๋Š” ํ•จ์ˆ˜ ๊ตฌํ˜„ํ•˜๊ธฐ
150์„ ์ดˆ๊ณผํ•˜๋ฉด '๋งค์šฐ ๋‚˜์จ'
81 ~ 150 ์ด๋ฉด '๋‚˜์จ'
31 ~ 80 ์ด๋ฉด '๋ณดํ†ต'
1 ~ 30 ์ด๋ฉด '์ข‹์Œ'
0 ์ด๋ฉด '๊นจ๋—ํ•จ'

function fineDustDegree(degree) {
  if (degree > 150) {
    return '๋งค์šฐ ๋‚˜์จ';
  } else if (degree > 80) {
    return '๋‚˜์จ';
  } else if (degree > 30) {
    return '๋ณดํ†ต';
  } else if (degree > 0) {
    return '์ข‹์Œ';
  } else if (degree === 0) {
    return '๊นจ๋—ํ•จ';
  }
}

23. ์›”๋ฆฌ๋ฅผ ์ฐพ์•„๋ผ

x์ถ•์ด 100์ด๊ณ  y์ถ•์ด 100์ธ ์˜์—ญ ์•ˆ์—์„œ
x์ถ•์ด 0 ~ 50์ด๊ณ  y์ถ•์ด 0 ~ 50์ด๋ฉด

24. ์ง๊ฐ์‚ผ๊ฐํ˜•์˜ ํ•œ ๋ณ€์˜ ๊ธธ์ด ๊ตฌํ•˜๊ธฐ

Math.sqrt().toFixed(2)

profile
Der Vogel kรคmpft sich aus dem Ei ๐Ÿฅš๐Ÿฃ ๋ชฉํ‘œ๋ฅผ ์œ„ํ•ด ๋Š์ž„์—†์ด ์ž์‹ ์˜ ์„ธ๊ณ„๋ฅผ ๊นจ๋œจ๋ฆด ์ˆ˜ ์žˆ๋Š” ์šฉ๊ฐํ•œ ๊ฐœ๋ฐœ์ž๊ฐ€ ๋˜๊ณ  ์‹ถ์Šต๋‹ˆ๋‹ค.

0๊ฐœ์˜ ๋Œ“๊ธ€