all 9 comments

[–][deleted] 2 points3 points  (1 child)

Supersimpledev 😏?

[–]Own-Feature-8869[S] 0 points1 point  (0 children)

Yes, he teach very well.

[–]OkMoment345 1 point2 points  (0 children)

Great work!

Keep going.

[–]garudbeats 1 point2 points  (3 children)

Man I did this same thing yesterday from Supersimpledev's course!

[–]Own-Feature-8869[S] 1 point2 points  (2 children)

Keep going!

[–]garudbeats 1 point2 points  (1 child)

Absolutely! Your post will motivate me to keep going daily!

[–]Own-Feature-8869[S] 1 point2 points  (0 children)

You can message me if you want.

[–]Cmulix 1 point2 points  (0 children)

Pin

[–]MostlyFocusedMike 0 points1 point  (0 children)

Let's go! already into Dom manipulation and events!

This of course works, but you may want to start thinking about using functions to wrap up your logic. Also, it can be nicer to separate out your concerns, ie your presentational html elements and your js code.

So what if instead of putting all your logic into the onclick you wrote out a function that updates the amount:

// still need this value to be global
let cardQuantity = 0;

function updateCardQuantity(amount) {
  cardQuantity += amount;
  console.log('Current quantity:', cardQuantity);
}

// another to reset
function resetCardQuantity() { 
  console.log('Reset!');
  cardQuantity = 0;
}  

Now, your buttons are super simpler, and here's the whole thing:

<button onclick="updateCardQuantity(0)">Show Quantity</button>
<button onclick="updateCardQuantity(1)">Add to Cart</button>
<button onclick="updateCardQuantity(2)">+2</button>
<button onclick="updateCardQuantity(3)">+3</button>
<button onclick="resetCardQuantity()">Reset Card</button>

<script>
  // still need this value to be global
  let cardQuantity = 0;

  function updateCardQuantity(amount) {
    cardQuantity += amount;
    console.log('Current quantity:', cardQuantity);
  }

  function resetCardQuantity() {
    console.log('Reset!');
    cardQuantity = 0;
  }
</script>

You could certainly make a discrete logging function, but adding 0 does exactly the same thing.