Step 28 build a shopping cart not passing

hi, totally blind using jaws 2026 screen reader. now up to step 28 of the shopping cart and have done a bunch of lesson resets, hard refreshes, and also wrote the code several times for the step 28, but it is not passing, fcc is very strict. wish it was a bit more liberal. but that would be too much work to rewrite the fcc system. and now also dealing with accessbiility challenges, and my code is correct as far as i know, did find a current version some one else had poste on the forum, a while back and the code looks the same or very similar. so is it my code or the fcc playing, so have tried a few times to get it to pass and not passing, so just frustrated. and i cannot see the if any hidden characters or hidden spacing as rely on text to speech and a screen reader. rant over. so heres my js code, the error message and a link to the step. if any one can help me out and where i am going wrong to get it to pass and move on. that means waiting for most of the night or tomorrow as in adelaide, australia.
big sigh. hate waiting and have tried everything else. so any one in australia, south pacific, new zealand or asia who can help me out.
one blind developer on the forum, i know.
thank you.
marvin.
ps: pasting my code below.

java script:

Menu


// Step 1: get container element
const dessertCards = document.getElementById("dessert-card-container");

// Step 2: products array
const products = [
  { id: 1, name: "Vanilla Cupcakes (6 Pack)" },
  { id: 2, name: "French Macaron" },
  { id: 3, name: "Pumpkin Cupcake" },
  { id: 4, name: "Chocolate Cupcake" },
  { id: 5, name: "Chocolate Pretzels (4 Pack)" },
  { id: 6, name: "Strawberry Ice Cream" },
  { id: 7, name: "Chocolate Macarons (4 Pack)" },
  { id: 8, name: "Strawberry Pretzel" },
  { id: 9, name: "Butter Pecan Ice Cream" },
  { id: 10, name: "Rocky Road Ice Cream" },
  { id: 11, name: "Vanilla Macarons (5 Pack)" },
  { id: 12, name: "Lemon Cupcakes (4 Pack)" }
];

// Step 3: display products
dessertCards.innerHTML = "";
products.forEach(({ name, id }) => {
  dessertCards.innerHTML += `
    <div class="dessert-card">
      <h2>${name}</h2>
      <span id="product-count-for-id${id}"></span>
      <button class="add-to-cart-btn" data-id="${id}">Add to cart</button>
    </div>
  `;
});

// Step 4: ShoppingCart class
class ShoppingCart {
  constructor() {
    this.items = [];
  }

  addItem(id) {
    const product = products.find(p => p.id === id);
    if (!product) return;

    this.items.push(product);

    // Count items per product
    const countPerProduct = {};
    this.items.forEach(item => {
      countPerProduct[item.id] = (countPerProduct[item.id] || 0) + 1;
    });

    // ✅ Step 28: exact variable name FCC expects
    const currentProductCountSpan = document.getElementById(`product-count-for-id${product.id}`);
    if (currentProductCountSpan) {
      currentProductCountSpan.textContent = countPerProduct[product.id] > 1 ? `${countPerProduct[product.id]}x` : '';
    }
  }
}

// Step 5: initialize cart and attach single listener
const cart = new ShoppingCart();

dessertCards.addEventListener("click", e => {
  if (e.target.classList.contains("add-to-cart-btn")) {
    const id = parseInt(e.target.dataset.id, 10);
    cart.addItem(id);
  }
});

error:
You should access the textContent property of currentProductCountSpan .
link to the step:

hello marvin,

  • you can try this even more simplistically
  • you already have ‘required variables’ defined beforehand, you just simply make use of ‘span’ element within ternary operator using ‘string template’ directly for truthy expression and leaving falsy expression as it is

so my two cents would be restart this step and simply make use of ‘span’ element within ternary operator thruthy expression and try again, happy coding :slight_smile:

hi, tried your suggestion, rewrote the function in the html and the js, but still not passing and using a screen reader, so cannot see if any hidden characters or trailling aspaces. so is it my code or the fcc, have tried a lot of times, but it is not passing, so will paste my code, htnml, js and the error below.
help!
thank you.
marvin.
ps: pasting below.

html:

<!DOCTYPE html>

<html lang="en">
<head>
  <meta charset="UTF-8" />
  <meta http-equiv="X-UA-Compatible" content="IE=edge" />
  <meta name="viewport" content="width=device-width, initial-scale=1.0" />
  <title>freeCodeCamp Shopping Cart</title>
  <link rel="stylesheet" href="styles.css" />
  <style>
    body { font-family: Arial, sans-serif; padding: 20px; }
    .dessert-card { border: 1px solid #ccc; padding: 10px; margin: 10px 0; }
    .btn { padding: 5px 10px; cursor: pointer; }
    #cart-container { display: none; margin-top: 20px; border: 1px solid #888; padding: 10px; }
    .cart-item { margin: 5px 0; }
  </style>
</head>
<body>
  <header>
    <h1 class="title">Desserts Page</h1>
  </header>
  <main>
    <button id="cart-btn" type="button" class="btn">
      <span id="show-hide-cart">Show</span> Cart
    </button>


<div id="cart-container">
  <button class="btn" id="clear-cart-btn">Clear Cart</button>
  <div id="products-container"></div>
  <p>Total number of items: <span id="total-items">0</span></p>
  <p>Subtotal: <span id="subtotal">$0.00</span></p>
  <p>Taxes: <span id="taxes">$0.00</span></p>
  <p>Total: <span id="total">$0.00</span></p>
</div>

<div id="dessert-card-container"></div>


</main>

<script src="script.js"></script>

</body>
</html>
<!DOCTYPE html>

<html lang="en">

<head>

  <meta charset="UTF-8" />

  <meta http-equiv="X-UA-Compatible" content="IE=edge" />

  <meta name="viewport" content="width=device-width, initial-scale=1.0" />

  <title>freeCodeCamp Shopping Cart</title>

  <link rel="stylesheet" href="styles.css" />

  <style>

    body { font-family: Arial, sans-serif; padding: 20px; }

    .dessert-card { border: 1px solid #ccc; padding: 10px; margin: 10px 0; }

    .btn { padding: 5px 10px; cursor: pointer; }

    #cart-container { display: none; margin-top: 20px; border: 1px solid #888; padding: 10px; }

    .cart-item { margin: 5px 0; }

  </style>

</head>

<body>

  <header>

    <h1 class="title">Desserts Page</h1>

  </header>

  <main>

    <button id="cart-btn" type="button" class="btn">

      <span id="show-hide-cart">Show</span> Cart

    </button>



    <div id="cart-container">

      <button class="btn" id="clear-cart-btn">Clear Cart</button>

      <div id="products-container"></div>

      <p>Total number of items: <span id="total-items">0</span></p>

      <p>Subtotal: <span id="subtotal">$0.00</span></p>

      <p>Taxes: <span id="taxes">$0.00</span></p>

      <p>Total: <span id="total">$0.00</span></p>

    </div>



    <div id="dessert-card-container"></div>

  </main>



  <script src="script.js"></script>

</body>

</html>
javascript:// DOM Elements
const cartContainer = document.getElementById("cart-container");
const productsContainer = document.getElementById("products-container");
const dessertCards = document.getElementById("dessert-card-container");
const cartBtn = document.getElementById("cart-btn");
const clearCartBtn = document.getElementById("clear-cart-btn");
const totalNumberOfItems = document.getElementById("total-items");
const cartSubTotal = document.getElementById("subtotal");
const cartTaxes = document.getElementById("taxes");
const cartTotal = document.getElementById("total");
const showHideCartSpan = document.getElementById("show-hide-cart");

let isCartShowing = false;

// Products array
const products = [
  { id: 1, name: "Vanilla Cupcakes (6 Pack)", price: 12.99, category: "Cupcake" },
  { id: 2, name: "French Macaron", price: 3.99, category: "Macaron" },
  { id: 3, name: "Pumpkin Cupcake", price: 3.99, category: "Cupcake" },
  { id: 4, name: "Chocolate Cupcake", price: 5.99, category: "Cupcake" },
  { id: 5, name: "Chocolate Pretzels (4 Pack)", price: 10.99, category: "Pretzel" },
  { id: 6, name: "Strawberry Ice Cream", price: 2.99, category: "Ice Cream" },
  { id: 7, name: "Chocolate Macarons (4 Pack)", price: 9.99, category: "Macaron" },
  { id: 8, name: "Strawberry Pretzel", price: 4.99, category: "Pretzel" },
  { id: 9, name: "Butter Pecan Ice Cream", price: 2.99, category: "Ice Cream" },
  { id: 10, name: "Rocky Road Ice Cream", price: 2.99, category: "Ice Cream" },
  { id: 11, name: "Vanilla Macarons (5 Pack)", price: 11.99, category: "Macaron" },
  { id: 12, name: "Lemon Cupcakes (4 Pack)", price: 12.99, category: "Cupcake" }
];

// ShoppingCart Class
class ShoppingCart {
  constructor() {
    this.items = [];
    this.taxRate = 8.25;
  }

  addItem(id) {
    const product = products.find(p => p.id === id);
    if (!product) return;

    // Add product to internal array
    this.items.push(product);

    // Update per-product count in the DOM
    let countSpan = document.getElementById(`product-count-for-id${id}`);
    if (!countSpan) {
      const p = document.createElement("p");
      p.className = "cart-item";
      p.innerHTML = `<span id="product-count-for-id${id}">1</span> ${product.name}`;
      productsContainer.appendChild(p);
    } else {
      countSpan.textContent = parseInt(countSpan.textContent) + 1;
    }

    this.updateTotals();
  }

  updateTotals() {
    const subtotal = this.items.reduce((sum, item) => sum + item.price, 0);
    const taxes = subtotal * this.taxRate / 100;
    cartSubTotal.textContent = `$${subtotal.toFixed(2)}`;
    cartTaxes.textContent = `$${taxes.toFixed(2)}`;
    cartTotal.textContent = `$${(subtotal + taxes).toFixed(2)}`;
    totalNumberOfItems.textContent = this.items.length;
  }

  clearCart() {
    this.items = [];
    productsContainer.innerHTML = "";
    cartSubTotal.textContent = "$0.00";
    cartTaxes.textContent

error:
is it my code or something not getting, have been trying to get this to pass for a few hours. and not apssing. so what stupid or dumb thing am i doing.
You should remove the undefined from your truthy expression.

hi, a screen reader user, step 28 is not passing. and i ahve tried multiple angles. have reset the lesson, have done refreshes, even rewrote the function two or three times. have tried for the last 24 hours and the editor is not the most friendly for screen readers with their vague error messages. so whats the code to get this to pass, have done research and tried to look at other code samples, to see if my code is wrong. so have tried and just fed up, it does not pass. so can some one show me how to get it to pass, not to do it for me, but the accessibility hurdles i am facing using a screen reader. do want to learn, this is the worst step i have come across. so strict and picky, and how to get htis to work, so can you sho me how to get it to pass. pasting my vs html, js and the error message below. can any one help me out, are there other blind people on this forum or doing the course, have they encountered this accessibility hurdle. and how they got it to work, so banging my head against the brick wall, but no bricks falling, can any one help me out asap. or have to wait another day just because i live in australia. sorry for grovelling or being a pain, maybe you want me to go away na m,maybe just a pain. so rant over. so have tried and tried and going around in circles and totally frustrated. have tried everything else i know, but not passing, and so this is going to take me a month or two with accessibility barriers, as another 32 steps to go. so can any one help me? sorry for being a pain!
thank you.
marvin.
ps: pasting my code below and the error message and also trying to use control m for preformatted code but the forum is not easy to navigate with a screen reader.

html:
.<!DOCTYPE html>

<html lang="en">

<head>

  <meta charset="UTF-8" />

  <meta http-equiv="X-UA-Compatible" content="IE=edge" />

  <meta name="viewport" content="width=device-width, initial-scale=1.0" />

  <title>freeCodeCamp Shopping Cart</title>

  <link rel="stylesheet" href="styles.css" />

  <style>

    /\* Quick basic styling \*/

    body { font-family: Arial, sans-serif; padding: 20px; }

    .dessert-card { border: 1px solid #ccc; padding: 10px; margin: 10px 0; }

    .btn { padding: 5px 10px; cursor: pointer; }

    #cart-container { display: none; margin-top: 20px; border: 1px solid #888; padding: 10px; }

  </style>

</head>

<body>

  <header>

    <h1 class="title">Desserts Page</h1>

  </header>

  <main>

    <button id="cart-btn" type="button" class="btn">

      <span id="show-hide-cart">Show</span> Cart

    </button>



    <div id="cart-container">

      <button class="btn" id="clear-cart-btn">Clear Cart</button>

      <div id="products-container"></div>

      <p>Total number of items: <span id="total-items">0</span></p>

      <p>Subtotal: <span id="subtotal">$0.00</span></p>

      <p>Taxes: <span id="taxes">$0.00</span></p>

      <p>Total: <span id="total">$0.00</span></p>

    </div>



    <div id="dessert-card-container"></div>

  </main>



  <script src="script.js"></script>

</body>

</html>
javascript:// =======================
// DOM ELEMENTS
// =======================
const cartContainer = document.getElementById("cart-container");
const productsContainer = document.getElementById("products-container");
const dessertCards = document.getElementById("dessert-card-container");
const cartBtn = document.getElementById("cart-btn");
const clearCartBtn = document.getElementById("clear-cart-btn");
const totalNumberOfItems = document.getElementById("total-items");
const cartSubTotal = document.getElementById("subtotal");
const cartTaxes = document.getElementById("taxes");
const cartTotal = document.getElementById("total");
const showHideCartSpan = document.getElementById("show-hide-cart");

let isCartShowing = false;

// =======================
// PRODUCTS ARRAY
// =======================
const products = [
  { id: 1, name: "Vanilla Cupcakes (6 Pack)", price: 12.99, category: "Cupcake" },
  { id: 2, name: "French Macaron", price: 3.99, category: "Macaron" },
  { id: 3, name: "Pumpkin Cupcake", price: 3.99, category: "Cupcake" },
  { id: 4, name: "Chocolate Cupcake", price: 5.99, category: "Cupcake" },
  { id: 5, name: "Chocolate Pretzels (4 Pack)", price: 10.99, category: "Pretzel" },
  { id: 6, name: "Strawberry Ice Cream", price: 2.99, category: "Ice Cream" },
  { id: 7, name: "Chocolate Macarons (4 Pack)", price: 9.99, category: "Macaron" },
  { id: 8, name: "Strawberry Pretzel", price: 4.99, category: "Pretzel" },
  { id: 9, name: "Butter Pecan Ice Cream", price: 2.99, category: "Ice Cream" },
  { id: 10, name: "Rocky Road Ice Cream", price: 2.99, category: "Ice Cream" },
  { id: 11, name: "Vanilla Macarons (5 Pack)", price: 11.99, category: "Macaron" },
  { id: 12, name: "Lemon Cupcakes (4 Pack)", price: 12.99, category: "Cupcake" }
];

// =======================
// SHOPPING CART CLASS
// =======================
class ShoppingCart {
  constructor() {
    this.items = [];
    this.taxRate = 8.25;
  }

  addItem({ name, id, price, category }) {
    this.items.push({ name, id, price, category });

    let currentProductCountSpan = document.getElementById(`product-count-for-id${id}`);

    if (!currentProductCountSpan) {
      currentProductCountSpan = document.createElement("span");
      currentProductCountSpan.id = `product-count-for-id${id}`;
      currentProductCountSpan.textContent = "1";

      const p = document.createElement("p");
      p.className = "cart-item";
      p.appendChild(currentProductCountSpan);
      p.append(` ${name}`);

      productsContainer.appendChild(p);
    } else {
      currentProductCountSpan.textContent = parseInt(currentProductCountSpan.textContent) + 1;
    }
  } // <-- CLOSE addItem

  updateTotals() {
    const subtotal = this.items.reduce((sum, item) => sum + item.price, 0);
    const taxes = subtotal * this.taxRate / 100;
    cartSubTotal.textContent = `$${subtotal.toFixed(2)}`;
    cartTaxes.textContent = `$${taxes.toFixed(2)}`;
    cartTotal.textContent = `$${(subtotal + taxes).toFixed(2)}`;
    totalNumberOfItems.textContent = this.items.length;
  }

  clearCart() {
    this.items = [];
    productsContainer.innerHTML = "";
    cartSubTotal.textContent = "$0.00";
    cartTaxes.textContent = "$0.00";
    cartTotal.textContent = "$0.00";
    totalNumberOfItems.textContent = "0";
  }
}

// =======================
// CREATE CART INSTANCE
// =======================
const cart = new ShoppingCart();

// =======================
// GENERATE DESSERT CARDS
// =======================
products.forEach(product => {
  const card = document.createElement("div");
  card.className = "dessert-card";
  card.innerHTML = `
    <p>${product.name} - $${product.price.toFixed(2)}</p>
    <button id="product-${product.id}" class="add-to-cart-btn btn">Add to Cart</button>
  `;
  dessertCards.appendChild(card);
});

// =======================
// EVENT LISTENERS
// =======================
dessertCards.addEventListener("click", e => {
  if (e.target.classList.contains("add-to-cart-btn")) {
    const productId = parseInt(e.target.id.replace("product-", ""));
    const product = products.find(p => p.id === productId);
    cart.addItem(product);
    cart.updateTotals();
  }
});

clearCartBtn.addEventListener("click", () => {
  cart.clearCart();
});

cartBtn.addEventListener("click", () => {
  isCartShowing = !isCartShowing;
  cartContainer.style.display = isCartShowing ? "block" : "none";
  showHideCartSpan.textContent = isCartShowing ? "Hide" : "Show";
});

error message:

You should remove the undefined from your truthy expression.

  • this means in your “starter code” for this step there was a given “ternary” expression where both thruthy and falsy were undefined
  • this is basically you are suppose to solve here, in place of ‘undefined’ you need to make use of ‘span’ element ‘textcontent’ directly into it

i dont use screen reader so i cant really say how this must have been for you!! in general easiest way would be to “restart” and start with that given “ternary expression” and only change that “truthy’ expression for ‘span’ with ‘template literal’ and try again…. happy coding :slight_smile: