Placing Order For Cake - String & Math
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.
Note: You may verify the correctness of your function by invoking it from within console.log
console.log(OrderCake('5848ButterBlast485'));
Console Output:
Placing Order For Cake - String & Math
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.
Note: You may verify the correctness of your function by invoking it from within console.log
console.log(OrderCake('5848ButterBlast485'));
Console Output:
Solution:
function OrderCake(str) {
//fill the code
var weight= +str.slice(0,4);
// console.log(typeof(weight));
var total=Math.round(450*weight/1000);
var w=Math.round(weight/1000);
weight=Math.round(weight/1000);
var flavour=str.slice(4,str.length-3);
var orderId=str.slice(str.length-3,str.length);
return "Your order for "+weight+" kg "+flavour+" cake has been taken. You are requested to pay Rs. "+total+" on the order no "+orderId;
}
console.log(OrderCake('5848ButterBlast485'))