Placing Order For Cake - String & Math
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'));
script.js
function OrderCake(str) {
var a=Array();
var length=str.length;
var gms=parseInt(str[0])*1000+parseInt(str[1])*100+parseInt(str[2])*10+parseInt(str[3]);
var c=Math.ceil(gms/1000);
for(var i=4;i<length-3;i++)
{
a=a+str[i];
}
var q=parseInt(str[length-3])*100+parseInt(str[length-2])*10+parseInt(str[length-1]);
return "Your order for "+ c+" kg "+a+" cake has been taken. You are requested to pay Rs. "+Math.floor((gms*450)/1000)+" on the order no "+q;
}
No comments:
Post a Comment