私はこの本当にシンプルなピザ注文プログラムを書いたが、私は自分自身を繰り返しているような気がする。最適化のヒント?プログラムで自分自身を繰り返さないようにするための最適化
コード:
var orderCount = 0;
function takeOrder(crustType, topping) {
console.log('Order:' + crustType + ' ' + 'pizza' + ' ' + topping);
orderCount = orderCount + 1;
}
function getSubTotal(itemCount) {
return itemCount * 7.5;
}
function calculateVat(VAT) {
return VAT/10 * 2;
}
// List the orders
takeOrder('Thin Crust Pizza', 'with bacon');
takeOrder('Fat Crust Pizza', 'with pepporoni');
takeOrder('Medium Crust Pizza', 'with Vegi');
takeOrder('Medium Crust Pizza', 'with steak');
takeOrder('Medium Crust Pizza', 'with Sausage');
// List the Total Minus VAT
console.log(getSubTotal(orderCount));
//Stores the cost with and With out VAT
var totalNoVAT = getSubTotal(orderCount);
var totalYesVAT = getSubTotal(orderCount) + calculateVat(totalNoVAT);
//Log the cost of the VAT and Total Order Cost with VAT
console.log('The VAT on that order would be' + ' ' + calculateVat(totalNoVAT));
console.log('This give you a grand total of' + ' ' + totalYesVAT);
このような機能を使用しても、実際には非常に反復的な感じはしません。あなたは '' with ''文字列を '' takeOrder''関数に移したいかもしれません。関数を繰り返し呼び出す代わりに、配列の配列を使用してそれを繰り返すことができます。反復で、@vlazを – Bergi
にすると、OPは何度も関数を呼び出さなければならないことを意味します。彼はオブジェクトの配列としてparamsを受け入れる場合、それを削除することができます – Rajesh
こんにちはRajesh、返信ええ、より多くのちょうど意見/アドバイスのおかげで、謝罪私はそこに来ていることを確認します – kiannjie101