Minimize the cost of buying the Objects | Function that will calculate the price ; Calculate the number of items we can get for free ; Calculate the number of items we will have to pay the price for ; Calculate the price ; Driver code ; Calling function
function totalPay ( totalItems , priceOfOneItem , N , M ) { var freeItems = 0 , actual = 0 ; freeItems = totalItems / ( N + M ) ; actual = totalItems - freeItems ; var amount = actual * priceOfOneItem ; return amount ; } var T = 12 , P = 8 ; var N = 2 , M = 1 ; document . write ( " " + totalPay ( T , P , N , M ) ) ;