Question: Refactor my Javascript code to reduce cognitive complexity. The following is my code for a problem to represent a receipt printer that will print receipt

Refactor my Javascript code to reduce cognitive complexity.

The following is my code for a problem to represent a receipt printer that will print receipt according to the product barcodes.

Refactor my Javascript code to reduce cognitive complexity. The following is mycode for a problem to represent a receipt printer that will print

You can also add additional methods to avoid too much complexity, which I suppose will be more effective?

I think my biggest problem is I have too many nested code, which increase the cognitive code really a lot. Please simplify my code

export default class ReceiptPrinter { constructor (products) { this.products = [...products); } * Create a receipt according to the barcodes. The receipt is formatted using * the following template: * ================== Receipt ===================== * $product names X$count$ $units * =========== Total =================.. * $total prices * * The product name* should padding to 30 characters wide. * The *X$count** section should padding to 10 characters wide. * The *unit* should have no padding. * The products in the receipt should be ordered by *barcode*. * * The total price should be formatted by 2 decimal digits e.g. '2.00'. If the total price is negative, the total price should be 0.00. * * The line breaking character is ''n'' + Liu Xia, 5 months ago (liuxia] feat: initial commit. * @param (string[]} barcodes The barcodes. * @throws when one of the barcode is not supported. The message should be ""Unknown barcode: $barcodes."'. * @throws when you have selected too many products (larger than 5000 items). */ print (barcodes) { // TODO: Please implement the method // } // TODO: You can add additional function or even class if you want // barcode === product.barcode); if (!eachSelectedProduct) { throw new Error('Unknown barcode: What the hell is this.'); } recordProduct(eachSelectedProduct, product Count, selectedProducts); } } function recordProduct (product, product Count, selectedProducts) { if (!productCount.has(product)) { selectedProducts.push(product); product Count.set(product, 1); } else { product Count.set(product, productCount.get(product) + 1); } } function printInFormat (product Count, selectedProducts) { const sortedSelectedProducts = selectedProducts.sort((pi, p2) => pi.barcode.localeCompare(p2.barcode)); const totalPrice = selectedProducts. reduce (totalprice, p) => totalprice + p.price * product Count.get(p), 0); return '==================== Receipt ==================== ' (sortedSelectedProducts.length === 0 ? ' ' : getLines (sortedSelectedProducts, productCount)) + ======== Total ' *${totalPrice.toFixed(2)}"; } function getLines (sortedSelectedProducts, productCount) { let resultString = ''; for (const product of sortedSelectedProducts) { resultString += '${product.name.padEnd(30) }x${productCount.get(product).toString() .padEnd (9) }${product. unit} "; } return resultString; } export default class ReceiptPrinter { constructor (products) { this.products = [...products); } * Create a receipt according to the barcodes. The receipt is formatted using * the following template: * ================== Receipt ===================== * $product names X$count$ $units * =========== Total =================.. * $total prices * * The product name* should padding to 30 characters wide. * The *X$count** section should padding to 10 characters wide. * The *unit* should have no padding. * The products in the receipt should be ordered by *barcode*. * * The total price should be formatted by 2 decimal digits e.g. '2.00'. If the total price is negative, the total price should be 0.00. * * The line breaking character is ''n'' + Liu Xia, 5 months ago (liuxia] feat: initial commit. * @param (string[]} barcodes The barcodes. * @throws when one of the barcode is not supported. The message should be ""Unknown barcode: $barcodes."'. * @throws when you have selected too many products (larger than 5000 items). */ print (barcodes) { // TODO: Please implement the method // } // TODO: You can add additional function or even class if you want // barcode === product.barcode); if (!eachSelectedProduct) { throw new Error('Unknown barcode: What the hell is this.'); } recordProduct(eachSelectedProduct, product Count, selectedProducts); } } function recordProduct (product, product Count, selectedProducts) { if (!productCount.has(product)) { selectedProducts.push(product); product Count.set(product, 1); } else { product Count.set(product, productCount.get(product) + 1); } } function printInFormat (product Count, selectedProducts) { const sortedSelectedProducts = selectedProducts.sort((pi, p2) => pi.barcode.localeCompare(p2.barcode)); const totalPrice = selectedProducts. reduce (totalprice, p) => totalprice + p.price * product Count.get(p), 0); return '==================== Receipt ==================== ' (sortedSelectedProducts.length === 0 ? ' ' : getLines (sortedSelectedProducts, productCount)) + ======== Total ' *${totalPrice.toFixed(2)}"; } function getLines (sortedSelectedProducts, productCount) { let resultString = ''; for (const product of sortedSelectedProducts) { resultString += '${product.name.padEnd(30) }x${productCount.get(product).toString() .padEnd (9) }${product. unit} "; } return resultString; }

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!