일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
1 | ||||||
2 | 3 | 4 | 5 | 6 | 7 | 8 |
9 | 10 | 11 | 12 | 13 | 14 | 15 |
16 | 17 | 18 | 19 | 20 | 21 | 22 |
23 | 24 | 25 | 26 | 27 | 28 |
- array
- Kotlin
- 오버라이딩
- Linux
- JS
- Java
- scrollview
- stylesheet
- 랜덤넘버
- 리액트
- SpringBoot
- nodejs
- npm
- TypeScript
- Android
- TextView
- Javscript
- 코틀린
- 자바스크립트
- 랜덤번호
- Hook
- React
- RecyclerView
- 안드로이드
- 스프링부트
- button
- fragment
- GoogleMaps
- JavaScript
- 구글맵스
- Today
- Total
타닥타닥 개발자의 일상
자바스크립트 함수/배열 이용해서 배열에 원하는 값 저장하기 array 본문
스티븐은 팁 계산기를 만들고 있다. 팁 계산기의 규칙은 아래와 같다.
계산서 가격이 50불에서 300불 사이이면 팁이 15% 적용된다. 만약 그렇지 않다면 팁은 20% 적용된다.
Steven is still building his tip calculator, using the same rules as before:
Tip 15% of the bill if the bill value is between 50 and 300, and if the value is different, the tip is 20%.
Test data: 125, 555 and 44
Your tasks:
1.
calcTip이라는 함수를 만들어서 값을 넣으면 위의 설정에 상응하는 팁을 반환하도록 하라.
Write a function 'calcTip' that takes any bill value as an input and returns the corresponding tip, calculated based on the rules above (you can check out the code from first tip calculator challenge if you need to). Use the function type you like the most.
const calcTip = function(bill){
return bill>=50 && bill<=300 ? bill*0.15 : bill*0.2;
}
2. bills라는 이름의 array를 사용해서 test data에 있는 bills의 값을 저장하자
And now let's use arrays! So create an array 'bills' containing the test data below
const bills = [125,555,44];
3. 각 가격에 해당하는 tip 금액을 계산하고, 그에 대한 tip 금액을 tips라는 array에 저장하자.
Create an array 'tips' containing the tip value for each bill, calculated from the function you created before
const tips = [calcTip(bills[0]),calcTip(bills[1]),calcTip(bills[2])];
console.log(tips);
4. total이라는 array를 생성하고 그곳에 가격과 팁을 합한 금액을 저장하자
Bonus: Create an array 'total' containing the total values, so the bill + tip
const total =[(bills[0]+tips[0]),(bills[1]+tips[1]),(bills[2]+tips[2])];
console.log(total);
문제 출처: Udemy 강의 The Complete JavaScript Course 2023 : From Zero to Export
'코딩 기록 > JavaScript' 카테고리의 다른 글
javascript / DOM 언어 eventListener 사용하여 JS 페이지에서 모달창 띄우기, esc키로 모달창 닫기, overlay로 모달창 이외 부분 어둡게하기 (0) | 2023.01.16 |
---|---|
배열을 받고 형식을 갖춘 문자로 출력하기 javascript JS (0) | 2022.12.06 |
자바 스크립트 if 조건문으로 함수 만들어서 원하는 결과 출력하기 (0) | 2022.11.16 |
JavaScript 기초 - 변수와 데이터 타입 : let / const / number / string/ boolean / null / undefine / symbol (0) | 2022.08.11 |
오픈 소스 pagination 이용하여 웹페이지 페이징하기 / 페이지 나누기 / javascript (0) | 2022.02.25 |