Question: Need help on a javascript problem regarding the null and undefined. The problem is about calculating total price according to the selected product ids. And

Need help on a javascript problem regarding the null and undefined.

The problem is about calculating total price according to the selected product ids. And the 'id' list can contain duplicated ids.

I've already implemented the following class (in the file receipt-calculator):

Need help on a javascript problem regarding the null and undefined. The

But I stuck at two tests (the ones mark as red), which is "deal with duplicated products" and "check if the product is null or undefined":

I don't know how to deal with conditions that there are duplicated products, and I use "variable === null" to check the null or undefined condition (as I shown above), the error "Cannot read property 'includes' of null" jump out.

Please help me with this problem

problem is about calculating total price according to the selected product ids.

I've given the following code text so it is easier for you to copy and paste:

[receipt-calculator.js]

class RecepitCalculator { constructor (products) { this.products = [...products]; } getTotalPrice (selectProductIds) { // TODO: Please implement the method //  selectProductIds.includes(product.id) ); if (selectProductIds === null) { throw new Error('Please provide selected product ids.'); } if (selectProductIds.length !== 0 && selectProducts.length === 0) { throw new Error('Product not found.'); } return selectProducts.reduce( (totalPrice, product) => totalPrice + product.price, 0 ); // --end-> } } export default RecepitCalculator; 

[receipt-calculator.test.js]

import ReceiptCalculator from './receipt-calculator'; describe('when calculating receipt', () => { const products = [ { id: 'BAX003', name: 'Coca Cola', price: 3 }, { id: 'BAX021', name: 'Pepsi', price: 4 }, { id: 'BAX303', name: 'Dr. Pepper', price: 2 } ]; it('should calculate total prices when select duplicated products', () => { const calculator = new ReceiptCalculator(products); const totalPrice = calculator.getTotalPrice(['BAX003', 'BAX021', 'BAX003']); expect(totalPrice).toEqual(10); }); it('should throw if selected product is null or undefined', () => { const calculator = new ReceiptCalculator(products); expect(() => calculator.getTotalPrice(null)).toThrow('Please provide selected product ids.'); expect(() => calculator.getTotalPrice(undefined)).toThrow('Please provide selected product ids.'); });

class RecepitCalculator { constructor (products) { this.products = [...products]; } \** Calculate total price according to the selected product ids. Please note that ...*/ getTotalPrice (select ProductIds : string[] ) { // TODO: Please implement the method // selectProductIds, includes (product.id) ); if (selectProductIds === null) { throw new Error('Please provide selected product ids.'); } if (selectProductIds.Length !== 0 && selectProducts.length === 0) { throw new Error('Product not found.'); } return selectProducts.reduce (totalPrice :/, product :1) => totalPrice + product.price, ); // --end-> } // TODO: // // You can add additional helper functions if you want // } export default RecepitCalculator; import ReceiptCalculator from './receipt-calculator'; describe( name: 'when calculating receipt', fn: () => { const products = [ { id: 'BAXD03', name: 'Coca Cola', price: 3 }, {id: 'BAX021', name: 'Pepsi', price: 4}, { id: 'BAX303' name: 'Dr. Pepper', price: 2 } ]; it( name: 'should calculate total price', fn: () => {...}); it( name: 'should calculate total prices when select duplicated products', fn: () => { const calculator = new ReceiptCalculator (products); const totalPrice = calculator.getTotalPrice( selectProductIds: ['BAX003', 'BAX021', 'BAX003']); expect(totalPrice). toEgual( expected: 10); }); it( name: 'should calculate total prices when select none', fn: () => {...}); it( name: 'should throw if selected product is null or undefined', fn: () => { const calculator = new ReceiptCalculator (products); expect( actual: () => calculator.getTotalPrice( selectProductIds: null)).toThrow(error: 'Please provide selected product ids.'); expect( actual: () => calculator.getTotalPrice( selectProductIds: undefined)).toThrow( error: 'Please provide selected product ids.'); }); it( name: 'should throw if product is not found', fn: () => {...}); -})

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!