Click here to Skip to main content
15,891,431 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
See more:
/* Create a function named cartProducts that takes in the product productId as an argument

cartProducts should get the correct product based on the productId
cartProducts should then increase the product's quantity
if the product is not already in the cart, add it to the carty array */
/////////////////////////

<pre lang="Javascript">let products = [
      {
        name: "juice",
        price: 2.50,
        quantity: 500,
        productId: 1,
      },
    
      {
        name: "water",
        price: 2.50,
        quantity: 1000,
        productId: 2,
      },
    
      {
        name: "milk",
        price: 1,
        quantity: 5000,
        productId: 3
      },
    ];
    
    const carty = [];


What I have tried:

function addProductToCart(productId){} but im not sure what to go from, tried everything but cant seem to work
Posted
Updated 1-Aug-22 1:17am
v7
Comments
Richard MacCutchan 31-Jul-22 9:16am    
"tried everything but cant seem to work"
Had you tried 'everything' then you would have the solution.

So show us the code that you have tried and explain what the problem is, and people will try to help you.
Dave Kreskowiak 31-Jul-22 10:51am    
Well, you've shown the function header, but nothing in the way of any code in the function.

You say you've tried "everything", but yet posting an empty function says you've tried nothing.
Richard MacCutchan 1-Aug-22 7:33am    
Please do not replace your question with rubbish characters, or otherwise hide it. That makes all the other comments and solutions difficult to understand.

1 solution

While we are more than willing to help those that are stuck, that doesn't mean that we are here to do it all for you! We can't do all the work, you are either getting paid for this, or it's part of your grades and it wouldn't be at all fair for us to do it all for you.

So we need you to do the work, and we will help you when you get stuck. That doesn't mean we will give you a step by step solution you can hand in!
Start by explaining where you are at the moment, and what the next step in the process is. Then tell us what you have tried to get that next step working, and what happened when you did.

If you are having problems getting started at all, then this may help: How to Write Code to Solve a Problem, A Beginner's Guide[^]
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900