Placing Order For Cake - String & Math CTS Program

Placing Order For Cake – String & Math Cognizant Handson Program

Write a JavaScript program to place an order for cake & to calculate its price. The function OrderCake must take a string as its argument, filter out the weight in grams required, the flovour of choice & the order id from the string using String functions, calculate the price & return the same appended to a string (refer to the console output).

The first 4 characters of the string argument to function must represent the cake’s weight in grams.

This must be followed by the flavor of choice and the last 3 characters must be number representing the order id.

Convert the weight in grams to weight in kgs and round the same using Math function. Consider Rs.450 as the cost per kg and round the calculated price too.

script.js 

function OrderCake(str) {
	matches = str.match(/\d+/g);
            var weight_in_gm = matches[0];
            var order_id = matches[1]

            var weight_notRound = Number(weight_in_gm/1000);
            var weight = Math.round(weight_notRound)
            var p = Number(weight_notRound*450);
            var price = Math.round(p)
            
            var first = str.split(weight_in_gm)

            var flavour = first[1].split(matches[1])[0]

            var newStr = "Your order for "+ weight + " kg "+ flavour + " cake has been taken. You are requested to pay Rs. "+price+
                        " on the order no "+ order_id+ ""
            return newStr
}

Similar Posts