node.js - JavaScript function; works locally but not for Lambda Minibootcamp -
this driving me crazy. appreciated. situation:
an existing object, storeitem has 2 relevant properties, price, , discountpercentage. write function (from outside of object), called addcalculatediscountprice, adds method, called calculatediscountprice storeitem returns discounted price.
here code:
function addcalculatediscountpricemethod(storeitem) { // add method storeitem object called 'calculatediscountprice' // method should multiply storeitem's 'price' , 'discountpercentage' discount // method subtracts discount price , returns 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; }; }
this part of lambda javascript mini bootcamp, has me install npm inside of each assignment directory after cloning git. when run same code (with relevant existing object, of course) using jsnode in terminal, expected output of 16 when using example variables in comments. however, when run npm test following errors:
fail tests/test.js ● addcalculatediscountpricemethod(storeitem) › should add method 'calculatediscountprice' store item object typeerror: cannot read property 'calculatediscountprice' of undefined @ object.<anonymous> (tests/test.js:209:64) ● addcalculatediscountpricemethod(storeitem) › should return discount price new 'calculatediscountprice' method
i have rewritten code multiple times trying make pass npm test. i've tried using bracket notation returning simple calculation on 1 single line (return price - (price * discountpercentage)), and, in addition original attempt, both of these retries worked fine on live terminal nodejs session.
so why isn't working npm test? not seeing?
update: here relevant test code npm test file:
describe('addcalculatediscountpricemethod(storeitem)', function() { var storeitem = { price: 80, discountpercentage: 0.1 }; var storeitem2 = { price: 5, discountpercentage: 0.5 }; it('should add method \'calculatediscountprice\' store item object', function() { expect(exercises.addcalculatediscountpricemethod(storeitem).calculatediscountprice).tobedefined(); expect(exercises.addcalculatediscountpricemethod(storeitem2).calculatediscount price).tobedefined(); }); it('should return discount price new \'calculatediscountprice\' method', function() { expect(exercises.addcalculatediscountpricemethod(storeitem).calculatediscountprice()).tobe(72); expect(exercises.addcalculatediscountpricemethod(storeitem2).calculatediscountprice()).tobe(2.5); }); });
the output telling storeitem undefined, since function argument , doing nothing before trying add function issue must test itself.
look @ test file , check if arguments being passed function. or use
console.log(arguments);
inside function, arguments reserved word contains parameters passed function call.
edit: looking @ test chaining 2 calls
addcalculatediscountpricemethod(storeitem) .calculatediscountprice()
since not returning calling calculatedisconutprice on undefined therefore must return storeitem in addcalculatediscountpricemethod.
Comments
Post a Comment