Learning JavaScript Logic with Arrays and Functions

Day 2 of Learning JavaScript 🚀 Today I stopped memorizing syntax and focused on actual logic. What I learned (and actually understood, not just “seen”): How arrays of objects work in real use cases Using find() to locate a specific item Using filter() to remove unwanted data Using reduce() to calculate values like total price Writing simple functions instead of messy code I built a basic cart logic: Increase product quantity Remove items with zero quantity Calculate total price dynamically No frameworks. No shortcuts. Just pure JavaScript and logic. I realized one thing today: 👉 If logic is weak, frameworks won’t save you. Tomorrow: more practice, cleaner code, fewer mistakes. #JavaScript #LearningInPublic #WebDevelopment #FrontendDevelopment #Consistency #Day2 #code // Day 2 JavaScript Practice // Topic: Array methods + basic logic let cart = [ { name: "Shoes", price: 1000, qty: 2 }, { name: "Bag", price: 500, qty: 1 }, { name: "Bottle", price: 500, qty: 0 } ]; // Increase quantity of a product function increaseQty(productName) { const item = cart.find(p => p.name === productName); if (item) { item.qty++; } } // Remove items with quantity 0 function removeZeroQtyItems() { cart = cart.filter(item => item.qty > 0); } // Calculate total price function calculateTotal(cartItems) { return cartItems.reduce((total, item) => { return total + item.price * item.qty; }, 0); } // ---- Execution ---- increaseQty("Shoes"); // Shoes qty becomes 3 removeZeroQtyItems(); // Bottle removed const totalPrice = calculateTotal(cart); console.log("Final Cart:", cart); console.log("Total Price:", totalPrice);

Ranjit, the content is good, but adding proper spacing would significantly improve readability.

To view or add a comment, sign in

Explore content categories