Question

In: Computer Science

The goal is to reduce the size of the render method's return expression (and perhaps the...

The goal is to reduce the size of the render method's return expression (and perhaps the render method itself) by moving some of the JSX expressions into separate blocks of code. You should use descriptive names for your JavaScript variables or functions.

import React, { Component } from 'react';
import styles from './App.module.scss';

function formatPrice(price) {
const dollars = Math.floor(price / 100)
let cents = `${price % 100}`.padEnd(2, '0')
return `$${dollars}.${cents}`
}

class App extends Component {

render() {
const { cart } = this.props;

return (
<section className={styles.App}>
<h1>Shopping Cart</h1>
<div className={styles['product-list']}>
<>
<div className={styles.product + ' ' + styles.header}>
<h3>Item</h3>
<h3>Description</h3>
<h3>Quantity</h3>
<h3>Price</h3>
<h3>Total</h3>
</div>
{
cart.map(item => (
<div key={item.id} className={styles.product}>
<p>{item.name}</p>
<p>{item.description}</p>
<p>{item.quantity}</p>
<p>{formatPrice(item.price)}</p>
<p>{formatPrice(item.quantity * item.price)}</p>
</div>
))
}
<hr />
<div className={styles.product + ' ' + styles.header}>
<p></p>
<p></p>
<p></p>
<h3>Total</h3>
<h3 style={{ color: "black"}}>
{formatPrice(cart.reduce((total, item) => total + item.quantity * item.price, 0))}
</h3>
</div>
</>
</div>
</section>
);
}
}

export default App;

Solutions

Expert Solution

import React, { Component } from 'react';
import styles from './App.module.scss';

function formatPrice(price) {
const dollars = Math.floor(price / 100)
let cents = `${price % 100}`.padEnd(2, '0')
return `$${dollars}.${cents}`
}
function getHeaders(){
        return <div className={styles.product + ' ' + styles.header}>
                        <h3>Item</h3>
                        <h3>Description</h3>
                        <h3>Quantity</h3>
                        <h3>Price</h3>
                        <h3>Total</h3>
                        </div>
}
class App extends Component {

render() {
const { cart } = this.props;

return (
<section className={styles.App}>
<h1>Shopping Cart</h1>
<div className={styles['product-list']}>
<>
{getHeaders()}
{
cart.map(item => (
<div key={item.id} className={styles.product}>
<p>{item.name}</p>
<p>{item.description}</p>
<p>{item.quantity}</p>
<p>{formatPrice(item.price)}</p>
<p>{formatPrice(item.quantity * item.price)}</p>
</div>
))
}
<hr />
<div className={styles.product + ' ' + styles.header}>
<p></p>
<p></p>
<p></p>
<h3>Total</h3>
<h3 style={{ color: "black"}}>
{formatPrice(cart.reduce((total, item) => total + item.quantity * item.price, 0))}
</h3>
</div>
</>
</div>
</section>
);
}
}

export default App;

NOTE : PLEASE COMMENT BELOW IF YOU HAVE CONCERNS.

I AM HERE TO HELP YOUIF YOU LIKE MY ANSWER PLEASE RATE AND HELP ME IT IS VERY IMP FOR ME


Related Solutions

The goal of this measure is to reduce the risk in the banking sector.
The goal of this measure is to reduce the risk in the banking sector.a. Monetary policy.b. Reserve Requirement.c. The money multiplierd. None of the above.
The Equilibrium-Constant Expression Learning Goal: To understand the form and meaning of the equilibrium-constant expression. Chemical...
The Equilibrium-Constant Expression Learning Goal: To understand the form and meaning of the equilibrium-constant expression. Chemical reactions are usually not one-way trips from reactant to product. In fact, most chemical reactions are reversible to at least some extent. Products formed by the forward reaction may react with each other to regenerate the reactants. When reactants are mixed, they will begin to react at a forward reaction rate particular to that chemical reaction. As reactants are depleted and products are formed,...
What is the goal of Universal Basic Income? Does it reduce inequality? Does it Reduce poverty?...
What is the goal of Universal Basic Income? Does it reduce inequality? Does it Reduce poverty? What are some arguments for and against Universal basic income?
10. The goal of this measure is to reduce the risk in the banking sector.
10. The goal of this measure is to reduce the risk in the banking sector.a. Monetary policy.b. Reserve Requirement.c. The money multiplierd. None of the above.12. Two men have given up looking for a job. One of them gave up looking 20 days ago. The other gave up 45 days ago when he became hopeless. A third man is a part-time worker, but would like a full-time job. Which of the following statements is true?a. Under U3 unemployment, the two...
The goal of this measure is to reduce the risk in the banking sector. a. Monetary...
The goal of this measure is to reduce the risk in the banking sector. a. Monetary policy. b. Reserve Requirement. c. The money multiplier d. None of the above.
Suppose it is management's goal to reduce the number of employee grievances being filed in a...
Suppose it is management's goal to reduce the number of employee grievances being filed in a unionized organization. Describe three things the human resource office can do to help meet this goal.
Suppose it is management's goal to reduce the number of employee grievances being filed in a...
Suppose it is management's goal to reduce the number of employee grievances being filed in a unionized organization. Describe three things the human resource office can do to help meet this goal.
Find the expression for the electric field due to a finite size of a line charge...
Find the expression for the electric field due to a finite size of a line charge in a certain distance. Then derive an expression for the electric field due to an infinite size of a line charge from what you found for the finite charge. Show that you can come up with a same using the Gauss law.
If the primary goal is to reduce inflation, which of the following fiscal policy actions would...
If the primary goal is to reduce inflation, which of the following fiscal policy actions would be appropriate during a period of a rapidly increasing consumer price index? I. Reduce government expenditures for defense and nuclear energy research. II. Increase transfer payments to those most severely affected by the rising price index. III. Increase personal income tax rates.
What is the best way to reduce the average grain size in a metal speciman.
What is the best way to reduce the average grain size in a metal speciman.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT