Note that there are some explanatory texts on larger screens.

plurals
  1. POjavascript arrays inside an array
    primarykey
    data
    text
    <p>the issue is .: in the following code ... when i call the method "addtocart" ... it prints correctly when the array has only one element .... but if it has more than 1 element it givs error as shown in the pic: (Ignore the first "404" error)</p> <p><img src="https://i.stack.imgur.com/pfHSI.png" alt="enter image description here"></p> <pre><code> var counter=0; function addToCart(productName,productImageURL,productPrice) { alert("in addTOCART() method"); var cartArray=new Array(); cartArray[counter]=doTask(productName, productImageURL, productPrice); //cartArray.concat(doTask(productName, productImageURL, productPrice)); /*cartArray.push(doTask(productName, productImageURL, productPrice));*/ alert("came back to original method"); alert(cartArray.length); for ( var i = 0; i &lt; cartArray.length; i++) { var array_element = cartArray[i]; console.log(array_element.toString()); } alert(cartArray.toString()); counter++; } function doTask(productName,productImageURL,productPrice) { alert("inside java script"); var cartItem = new Array(); itemName=productName; itemPrice=productPrice; alert(itemPrice); itemImageURL=productImageURL; quantity=prompt("Please enter the quantity","1"); quantity=parseInt(quantity); alert(quantity+2); if (!isNaN( quantity )) { alert("yes its a number"); alert(itemImageURL); alert(itemPrice); alert(quantity); alert(itemName); alert(itemImageURL); totalAmount=itemPrice*quantity; alert(totalAmount); cartItem.push(itemImageURL,itemName,itemPrice,quantity,totalAmount); //alert(cartItem.toString()); console.log(cartItem.toString()); return cartItem; } </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload