Wednesday, 15 August 2012

javascript - Comparring Arrays with For Loops -


OK, so I'm working on it for a while. I am comparing the values ​​of two arrays using for loop. Every time the array known as a cart hits the number that can be found in the product array, it displays the information of the product array, every time it hits, I think my code is okay. (Although I may be wrong), but it is not displaying the values, so I think there is something wrong with the execution of the process there. Enter the code

  as follows () {var txt = "" var products = new Array (); Products [0] = {Name: "refrigerator", Price: 88.99, IMG: "IMG / refrigerator. JPG"}; Products [1] = {Name: "Microwave Oven", Price: 76.99, IMG: "IMG / Microwave. JPG"}; Products [2] = {Name: "Dishwasher", Price: 276.67, IMG: "IMG / Dishwasher. JPG"}; Var cart = new array (); Cart [0] = 2; Cart [1] = 0; Cart [2] = 1; Cart [3] = 1; Cart [4] = 0; Cart [5] = 1; Cart [6] = 2; Cart [7] = 2; For (var i = 0; i & lt; carts.length; i ++) {for (var j = 0; j & lt; products.length; j ++) {if (cart [i] == j ) {Txt + = products [J] .name + '' + products [j] .price + "img src = '" + products [j] .img + "'> & gt; Document.getElementById ("answer"). InnerHTML + txt}}}  

You You should compare j with the cart [i] otherwise you will not get anything

 for  (var i = 0; i & lt; Carts.length; i ++) {for (var j = 0; j & lt; products.length; j ++) {if (cart [i] == j) {txt = products [j] name + '' + Products [J.] .price + "& lt; img src = '" + products [J] .img + "' & gt;" Document.getElementById ("answer"). InnerHTML + txt}}}  

Your txt variable should be modified with = and no + = < / Code>

You should customize your code document.getElementById ("Answer") for example can be started globally.


No comments:

Post a Comment