0
私は今まで3週間しかJSを学習しておらず、私のエクササイでは特定の状況に陥っていました。これは私のコードです:JavaScriptは変数console.logで未定義と表示されます
'use strict';
var positions = [
{
title: 'Product_Title_1',
producer: {
name: 'Producer_Name_1',
deferPeriod: 10,
lot: 3
},
price: 10000
},
{
title: 'Product_Title_2',
producer: {
name: 'Producer_Name_2',
deferPeriod: 24,
lot: 14
},
price: 9200
},
{
title: 'Product_Title_3',
producer: {
name: 'Producer_Name_3',
deferPeriod: 5,
lot: 1
},
price: 57000
}
];
console.log('Task 1:');
console.log();
let order = 10; //declaring it here solves the problem in a way, but it is not the way it should work
let shipment;
function lotCalculator (positions, order) {
return shipment = {
lots: Math.ceil(order/positions.producer.lot),
total: Math.ceil(order/positions.producer.lot) * positions.producer.lot * positions.price
};
}
let result1 = lotCalculator (positions[1], 15);
console.log(result1);
console.log();
console.log(`Order for Product_Title_2: ${order} pcs, order lots: ${shipment.lots}, final cost ${shipment.total} Q`); //order returns undefined
私のコードは次のようん:それはロットサイズと順番にピースの数を比較します。注文サイズがそれを超える場合、注文するロット数を増やします。最終的なコストを計算します。
私の質問は次のとおりです。関数の前にorder変数を宣言せずに、すべてを正しく動作させるにはどうすればよいですか?
内部で使用されてから変数を防ぎます。 –
[あなただけのリンクではなく]質問テキストに関連コードを入れてください(https://meta.stackoverflow.com/q/254428/1048572)。 – Bergi
注文の使用は、console.logステートメントの$ {order}のみです。 console.logの前に宣言すれば十分です。 –