ABOUT ME

-

Today
-
Yesterday
-
Total
-
  • 자바스크립트 예제
    javascript 2020. 5. 22. 01:28

    자바스크립트를 이용해

    1)음식값을 입력받고

    2)음식의 가격범주에 따라 팁을 정하고

    3)음식값과 팁을 합쳐

    4)각각의 총합 (총 음식값, 총 팁, 음식과 팁을 합친 값의 총계)를 보여주는 예제를 만들어 보았다.

    Udemy - The Complete JavaScript Course 2020 : Build Real Projects!과정의 Challenge3 과정이다. 아래 코드에 문제와 내가 해결한 답을 모두 적었다.

    /*****************************
    * CODING CHALLENGE 3
    */
    
    /*
    John and his family went on a holiday and went to 3 different restaurants. 
    The bills were $124, $48 and $268.
    To tip the waiter a fair amount, John created a simple tip calculator (as a function). 
    He likes to tip 20% of the bill when the bill is less than $50, 
    15% when the bill is between $50 and $200, 
    and 10% if the bill is more than $200.
    In the end, John would like to have 2 arrays:
    1) Containing all three tips (one for each bill)
    2) Containing all three final paid amounts (bill + tip).
    (NOTE: To calculate 20% of a value, simply multiply it with 20/100 = 0.2)
    GOOD LUCK 😀
    */
    
    function tipRateCalculator(bill){
        var tipRate;
        
        console.log("tipRateCalculator - bill : ",bill);
        
    
        switch(true){
            case bill >= 0 && bill<50 :
                return tipRate = 0.2;
            case bill >= 50 && bill < 200 :
                return tipRate = 0.15;
            case bill >=200 :
                return tipRate = 0.1;
        }
    }
    
    function tipCalculator(bill){    
        var tip = bill*tipRateCalculator(bill);
        
        return tip;
    }
    
    function makeTotalBillArray(){
        var totalBillArray = new Array;
        do{
            var billQ = parseInt(prompt("please insert bill")); 
            
            totalBillArray.push(billQ);
            var question = confirm('Continue?');
        }while(question);
    
        return totalBillArray;
    }
    
    function makeTipArray(billArray){
        var tipArray = new Array;
    
        billArray.forEach(bill => {
            var tip = tipCalculator(bill);
            tipArray.push(tip);
        });
    
        return tipArray;
    }
    
    function billAddTip(billArray, tipArray){
        var calculateBillArray = new Array;
        for(var i = 0; i < billArray.length; i++){
            var added = billArray[i] + tipArray[i];
            calculateBillArray.push(added);
        }
        return calculateBillArray;
    }
    
    function makeCalculatedBillArray(){
        var totalBillArray = makeTotalBillArray();
        var tipArray = makeTipArray(totalBillArray);
        var calculateBillArray = billAddTip(totalBillArray,tipArray);
    
        var totalArray = new Array;
        totalArray.push(totalBillArray);
        totalArray.push(tipArray);
        totalArray.push(calculateBillArray);
    
        return totalArray;
    }
    
    function makeTotal(array){
        console.log("makeTotal",array);
        
        var total = new Number;
        array.forEach(element => {
            console.log(element);
            total += element;
        });    
        return total;
    }
    
    function staticsOfTotal(){
        var totalArray = makeCalculatedBillArray();
        var totalBill = makeTotal(totalArray[0]);
        var totalTip = makeTotal(totalArray[1]);
        var total = makeTotal(totalArray[2]);
    
        alert(`total Bill = ${totalBill}`);
        alert(`total Tip = ${totalTip}`);
        alert(`total = ${total}`);
    }
    
    staticsOfTotal();
    

     

    참조 : Udemy - The Complete JavaScript Course 2020 : Build Real Projects!

    'javascript' 카테고리의 다른 글

    자바스크립트 this  (0) 2020.05.22
    자바스크립트 Object  (0) 2020.05.22
    자바스크립트 Truthy and Falsy values, equality operators  (0) 2020.05.21
    switch(true)  (0) 2020.05.21
    자바스크립트 연산자 우선순위  (0) 2020.05.18

    댓글

Designed by Tistory.