Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 3 of 3
  1. #1
    New to the CF scene
    Join Date
    Aug 2012
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    javascript simple shopping basket help

    Hi, I am building a basic shopping basket and the problem I find is that all the products, even with quantity of 0 are being displayed in the shopping basket. Please can anyone help!.. I don't know what I'm doing wrong. Thank you so much in advance!
    This is my code:

    function displayProducts(){
    var quant = new Array (10);
    quant[1] = Number(getInput("prodA"));
    quant[2] = Number(getInput("prodB"));
    quant[3] = Number(getInput("prodC"));
    quant[4] = Number(getInput("prodD"));
    quant[5] = Number(getInput("prodE"));
    quant[6] = Number(getInput("prodF"));
    quant[7] = Number(getInput("prodG"));
    quant[8] = Number(getInput("prodH"));
    quant[9] = Number(getInput("prodI"));
    quant[10] = Number(getInput("prodJ"));
    var nameArray = new Array (name1, name2, name3, name4, name5, name6, name7, name8, name9, name10);
    var name1 = 'prodA';
    var name2 = 'prodB';
    var name3 = 'prodC';
    var name4 = 'prodD';
    var name5 = 'prodE';
    var name6 = 'prodFi';
    var name7 = 'prodG';
    var name8 = 'prodH';
    var name9 = 'prodI';
    var name10 = 'prodJ';
    for( var i=0; i<10; i++)
    {
    if (quant[i] > 0)
    {
    var product1 = quant[1] + ' ' + name1 + '</br>';
    var product2 = quant[2] + ' ' + name2 + '</br>';
    var product3 = quant[3] + ' ' + name3 + '</br>';
    var product4 = quant[4] + ' ' + name4 + '</br>';
    var product5 = quant[5] + ' ' + name5 + '</br>';
    var product6 = quant[6] + ' ' + name6 + '</br>';
    var product7 = quant[7] + ' ' + name7 + '</br>';
    var product8 = quant[8] + ' ' + name8 + '</br>';
    var product9 = quant[9] + ' ' + name9 + '</br>';
    var product10 = quant[10] + ' ' + name10 + '</br>';
    document.getElementById('shoppingBox').innerHTML = product1 + product2 + product3 + product4 + product5 + product6 + product7 + product8 + product9 + product10;
    }
    }
    }

  • #2
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    17,919
    Thanks
    203
    Thanked 2,531 Times in 2,509 Posts
    Homework? You seem to have made an effort so this ought to move you forward:-

    Code:
    <script type = "text/javascript">
    
    var quant = [];
    //  .....
    //for (var i = 0; i <=10; i++) {  // POPULATE THE ARRAY FOR TESTING
    //quant[i] = i;
    //}
    
    var prodname = [];
    prodname[1] = "prodA";
    prodname[2] = "prodB";
    prodname[3] = "prodC";
    prodname[4] = "prodD";
    prodname[5] = "prodE";
    prodname[6] = "prodF";
    prodname[7] = "prodG";
    prodname[8] = "prodH";
    prodname[9] = "prodI";
    prodname[10] = "prodJ";
    
    function displayProducts() {
    var totals="";
    for (var i=1; i<quant.length; i++) {
    if (quant[i] > 0) {
    totals+= quant[i] + " " + prodname[i] + "<br>";
    }
    }
    
    document.getElementById("shoppingBox").innerHTML = totals;
    }
    
    </script>
    Be aware that array indices start at 0, not 1, so your array quant has 11 elements, not 10.
    I don't see any function getInput() but the arguments should not be in quotes - without quotes is a variable, with quotes is a string literal. Make sure that you understand the difference. You need to check for NaN (not numeric input) or add ||0 to the expressions.

    Code:
    quant[1] = Number(getInput(prodA))  || 0;
    You seem to be confusing the actual names of the products (prodA = bicycle, prodB = toothbrush) with the Javascript variable names prodA, prodB etc. you have assigned to them.

    Quizmaster: In mythology, what term for a potion that increases sexual desire is derived from the name of the Greek goddess of love?
    Contestant: Viagra
    Last edited by Philip M; 08-06-2012 at 08:48 AM.

    All the code given in this post has been tested and is intended to address the question asked.
    Unless stated otherwise it is not just a demonstration.

  • #3
    New to the CF scene
    Join Date
    Aug 2012
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Thank you! It was very helpful!


  •  

    Tags for this Thread

    Posting Permissions

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