dcsimg
www.webdeveloper.com
Results 1 to 4 of 4

Thread: Help with code review

  1. #1
    Join Date
    Mar 2015
    Posts
    10

    Help with code review

    Please, could somebody help me with a code review? Many thanks in advance


    var categories = [];
    var products = [];
    var catalog = products;

    var
    Product = function (productID, category, name, price) {
    this.productID = productID;
    this.category = category;
    this.name = name;
    this.price = price;
    },
    Category = function (categoryID, name) {
    this.categoryID = categoryID;
    this.name = name;
    };

    var
    CartItem = function(itemID, product, quantity) {
    this.itemID = itemID;
    this.product = product;
    this.quantity = quantity;
    },
    ShoppingCart = function(cartID, items) {
    this.cartID = cartID;
    this.items = items;
    };

    var items = []; //collection of CartItem
    var shoppingCart = new ShoppingCart(1 ,items );

    //addCategoryToCatalog
    function addCategoryToCatalog (categoryID, name) {
    for (var i = 0; i < categories.length; i++) {
    if (categories[i].categoryID === categoryID) {
    return categories[i];
    }
    }
    var categoryItem = new Category(categoryID, name);
    categories.push(categoryItem);
    }
    addCategoryToCatalog(1, 'shoes');
    addCategoryToCatalog(3, 'shirts');


    //addProductToCatalog
    function addProductToCatalog(productID, category, name, price) {
    for (var i = 0; i < products.length; i++) {
    if(products[i].productID === productID) {
    return products[i];
    }
    }
    var productItem = new Product(productID, category, name, price);
    products.push(productItem);
    }
    addProductToCatalog(1, categories[0], 'boots', 400);
    addProductToCatalog(2, categories[1], 'Flannel Shirt', 47.90);
    addProductToCatalog(3, categories[0], 'Converse', 50.90);

    //addItemToCart
    function addItemToCart(itemID, product, quantity) {
    for (var i = 0; i < items.length; i++) {
    if(items[i].itemID === itemID) {
    items[i].quantity += quantity;
    return;
    saveCart();
    }
    }

    var item = new CartItem(itemID, product, quantity);
    items.push(item);
    }

    addItemToCart(1, products[0], 4);
    addItemToCart(2, products[1], 2);
    addItemToCart(4, products[0], 20);

    //removeItemfromCart(name) -> removes one item
    function removeItemFromCart(itemID, product, quantity) {
    for (var i = 0; i < items.length; i++) {
    if(items[i].itemID === itemID) {
    items[i].quantity--;
    if( items[i].quantity === 0) {
    items.splice(i, 1);
    }
    return;
    }
    }
    saveCart();
    }

    removeItemFromCart(1);
    removeItemFromCart(2);
    removeItemFromCart(4);


    function removeItemFromCartAll(itemID) {
    for (var i in items ) {
    if(items[i].itemID === itemID) {
    items.splice(i, 1);
    return;
    }
    }
    saveCart();
    }
    removeItemFromCartAll(1);


    //clearCart()
    function clearCart() {
    items = [];
    saveCart();
    }
    //clearCart();


    //countCart() -> return total count
    function countCart() {
    var totalCount = 0;
    for(var i in items) {
    totalCount += items[i].quantity;
    }
    return totalCount;
    }
    countCart();


    //totalCart() ->return total cost
    function totalCart() {
    var totalCost = 0;
    for(var i in items) {
    totalCost += items[i].product.price;
    }
    return totalCost;
    }
    totalCart();


    //listCart() -> returns an array of Item
    // function listCart() {
    // var cartCopy = [];
    // for(var i in items) {
    // var item = items[i];
    // var itemCopy = {};
    // for(var p in item) {
    // itemCopy[p] = item[p];
    // }
    // cartCopy.push(itemCopy);
    // }
    // return cartCopy;
    // }
    //console.log(listCart());

    //saveCart()
    function saveCart() {
    localStorage.setItem('items', JSON.stringify(items));
    }
    //loadCart()
    function loadCart() {
    var itemsCart = JSON.parse(localStorage.getItem('items'));
    return itemsCart;
    }
    loadCart();



    /* ============================================*/

    function renderCart() {
    var itemCopies = [];
    for(var i in items) {

    var $copy = $('.cart-selection').clone();

    $('ul', $copy).html('<li>' + items[i].itemID + ''+ items[i].product.category.categoryID + ''+ ''+ ''+ items[i].product.category.name + ''+ items[i].product.name +''+ ''+ items[i].product.price + ''+ items[i].product.productID +''+ items[i].quantity + '</li>');

    itemCopies.push($copy);
    $copy.appendTo($('.product-list'));
    }
    return itemCopies;
    }
    renderCart();

  2. #2
    Join Date
    Mar 2007
    Location
    localhost
    Posts
    5,539
    1. What is the problem with the code you are experiencing?
    2. If this is a JQuery thing, you need the JavaScript Frameworks forum.
    3. When posting code, please use forum BBCode tags.
    --> JavaScript Frameworks like JQuery, Angular, Node <--
    ... and please remember to wrap code with forum BBCode tags:-

    [CODE]...[/CODE] [HTML]...[/HTML] [PHP]...[/PHP]

    If you can't think outside the box, you will be trapped forever with no escape...

  3. #3
    Join Date
    Mar 2015
    Posts
    10
    Thank you for your answer. I don't have a specific issue with this code, I only wanted to ask for a second opinion and some suggestions on what I've done so far at my project(online shop).

  4. #4
    Join Date
    Mar 2007
    Location
    localhost
    Posts
    5,539
    For online shops, you are better off using a web form / server-side and then an AJAX option if avaiable, but remember that JavaScript is by no means secure and anything you have the client do will have to be validated serverside.

    If the user has no JavaSript enabled or a browser that doesn't support or full support JavaScript, then any baskets will fail, therefore the system would need to rely on a form / server-side option.
    --> JavaScript Frameworks like JQuery, Angular, Node <--
    ... and please remember to wrap code with forum BBCode tags:-

    [CODE]...[/CODE] [HTML]...[/HTML] [PHP]...[/PHP]

    If you can't think outside the box, you will be trapped forever with no escape...

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center