skip to Main Content

I am trying to make a shopping cart system for my website.

The System works as:

  • System fetches the Shopping Cart string and splits it into an array with ",".

  • System checks for the matching part inside of the array.

  • If the system finds the product; It prints "Product Found!"

  • If Not; the system prints "Product Not Found!"

I did this but the system always prints "Product Not Found!", how can I solve it?

The array that I use {1234567890,#000000,XL,1,100,1234567899,#000000,XL,2,100}

Array Pattern is ProductID,SelectedColor,SelectedSize,Product Quantity,Price.

var ShoppingCartArray = ShoppingCartValue.split(",");

for (var i = 0; i < ShoppingCartArray.length; i = i + 5) {
    var cartProductID = ShoppingCartArray[i];
    var cartColor = ShoppingCartArray[i + 1];
    var cartSize = ShoppingCartArray[i + 2];
    var cartQuantity = parseInt(ShoppingCartArray[i + 3]);
    var cartPrice = ShoppingCartArray[i + 4];

    console.log("Cart Product ID:", cartProductID);
    console.log("Cart Color:", cartColor);
    console.log("Cart Size:", cartSize);
    console.log("Cart Quantity:", cartQuantity);
    console.log("Cart Price:", cartPrice);

    if (
        cartProductID === ProductID &&
        cartColor === SelectedColor &&
        cartSize === SelectedSize &&
        cartPrice === Price
    ) {
        console.log("Product Found !");

        var ProductQuantity = cartQuantity;

        console.log("Quantity:", ProductQuantity);
    } else {
        console.log("Product Not Found !");
    }
}

2

Answers


  1. check if type of ProductID is same to cartProductID or convert both to string

    cartProductID.toString() === ProductID.toString()

    Login or Signup to reply.
  2. Write the question in a properway, this is not an array
    {1234567890,#000000,XL,1,100,1234567899,#000000,XL,2,100}.

    and require more details.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search