타닥타닥 개발자의 일상

자바스크립트 함수/배열 이용해서 배열에 원하는 값 저장하기 array 본문

코딩 기록/JavaScript

자바스크립트 함수/배열 이용해서 배열에 원하는 값 저장하기 array

NomadHaven 2022. 11. 16. 22:29

 

스티븐은 팁 계산기를 만들고 있다. 팁 계산기의 규칙은 아래와 같다.

계산서 가격이 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

Comments