これは私を狂わせてしまいます。どんな助けもありがとう。状況:JavaScript関数。ローカルでは動作しますが、Lambda Minibootcampでは動作しません。
既存のオブジェクトであるstoreItemには、2つの関連するプロパティpriceおよびdiscountPercentageがあります。私はaddCalculateDiscountPriceという関数を(オブジェクトの外側から)書き、calculateDiscountPriceというメソッドを追加して割引価格を返すstoreItemを作成します。ここで
は、コードは次のとおりです。
function addCalculateDiscountPriceMethod(storeItem) {
// add a method to the storeItem object called 'calculateDiscountPrice'
// this method should multiply the storeItem's 'price' and 'discountPercentage' to get the discount
// the method then subtracts the discount from the price and returns the discounted price
// example:
// price -> 20
// discountPercentage -> .2
// discountPrice = 20 - (20 * .2)
storeItem.calculateDiscountPrice = function() {
var discount = this.discountPercentage;
var saved = this.price * discount;
var finalPrice = this.price - saved;
return finalPrice;
};
}
これは私がのgitでそれをクローニングした後、各割り当てディレクトリ内NPMをインストールしているラムダJavaScriptのミニ合宿の一部です。 jsnodeを使ってこの同じコードを実行すると、コメント内の変数例を使用すると、16の期待される出力が得られます。私はこのコードにこのパスNPMのテストをしようと何度も書き直されている
FAIL tests/test.js
● addCalculateDiscountPriceMethod(storeItem) › should add the method 'calculateDiscountPrice' to the store item object
TypeError: Cannot read property 'calculateDiscountPrice' of undefined
at Object.<anonymous> (tests/test.js:209:64)
● addCalculateDiscountPriceMethod(storeItem) › should return the discount price from the new 'calculateDiscountPrice' method
:私はNPMテストを実行したときしかし、私は、次のエラーを取得します。私はブラケット記法を使用して、明らかに単純な計算を1行(返品価格 - (price * discountPercentage))に戻してみました。元の試行に加えて、両方の再試行はライブ端末でうまくいきましたnodejsセッション。
なぜnpmテストではうまくいかないのですか?私は何を見ていないのですか?
アップデート:ここでは、NPMのテストファイルから該当するテストコードです:
describe('addCalculateDiscountPriceMethod(storeItem)', function() {
var storeItem = {
price: 80,
discountPercentage: 0.1
};
var storeItem2 = {
price: 5,
discountPercentage: 0.5
};
it('should add the method \'calculateDiscountPrice\' to the store item object', function() {
expect(exercises.addCalculateDiscountPriceMethod(storeItem).calculateDiscountPrice).toBeDefined();
expect(exercises.addCalculateDiscountPriceMethod(storeItem2).calculateDiscount Price).toBeDefined();
});
it('should return the discount price from the new \'calculateDiscountPrice\' method', function() {
expect(exercises.addCalculateDiscountPriceMethod(storeItem).calculateDiscountPrice()).toBe(72);
expect(exercises.addCalculateDiscountPriceMethod(storeItem2).calculateDiscountPrice()).toBe(2.5);
});
});
Isidro上記のテストコードをご覧ください。 –
ソリューションで編集してください! –
Insidro私はあなたの説明を理解しますが、これを修正するために正確に何を書いていますか? –