0

I have an array of values "purchasedDiscount" that I parse from HTML. Outside of the loop it holds a value. Inside the loop I get output when using a 0 in the index. When I try to get the values using the counter "i" from the for loop I get "undefined". I have no idea why this is happening. I am using Prototype JS, but I feel this is a JS issue directly with my syntax (could be wrong).

    var purchasedDiscount = $$('.cm-purchased-discount > span.price');
    console.log("Purchased Discount: " + purchasedDiscount[0].innerHTML);

    for (i = 0; i < cmItems.length; i++) {
        console.log("i: " + i);
        console.log("length of loop: " + cmItems.length);
        console.log(parseFloat(stripPriceFormatting(purchasedPrice[i].innerHTML)));
        console.log("Purchased Discount 0: " + purchasedDiscount[0].innerHTML);
        console.log("Purchased Discount i where i is 0: " + purchasedDiscount[i].innerHTML);
    }

Here is my Console output

(index):1280 Purchased Discount: £0.00
(index):1287 i: 0
(index):1288 length of loop: 2
(index):1289 679
(index):1290 Purchased Discount 0: £0.00
(index):1291 Uncaught TypeError: Cannot read property 'innerHTML' of undefined

stripPriceFormatting Delcation

function stripPriceFormatting(price) {
    var priceFormats = ["$", "£"];
    var strippedFormatting = price;
    for (i=0; i < priceFormats.length; i++) {
        strippedFormatting = strippedFormatting.replace(priceFormats[i], "");
    }
    console.log("Stripped Formatting: " + strippedFormatting);
    return strippedFormatting.replace(",", "");
}
camdixon
  • 852
  • 2
  • 18
  • 33
  • 1
    could you show the declaration for `stripPriceFormatting` , maybe that's changing `i` – maioman Jul 04 '17 at 21:23
  • ahh, quite possible, but I assumed it would localize to that for loop. Will change the stripPriceFormatting loop to 'j' – camdixon Jul 04 '17 at 21:26
  • yeah that's it ! – maioman Jul 04 '17 at 21:27
  • Fixed, thanks! Can you post an answer ? – camdixon Jul 04 '17 at 21:28
  • You're making a single global variable called `i`. You need `for (var i = 0` in both cases. Don't use `j` instead, **fix your variable declarations**. – user229044 Jul 04 '17 at 21:31
  • I'd suggest using let instead of var though. – Angels Jul 04 '17 at 21:32
  • @Angels **in this case having block scoping isn't really necessary**, declaring the variable with `var` will scope it internally to the function, and that's enough. – maioman Jul 04 '17 at 21:35
  • meagar, can you describe how you would do this? Angels, what does "let" do? I'm looking it up now. How would it help me here? – camdixon Jul 04 '17 at 21:35
  • @maioman and what if he someday will make two for loops in the same function with 'i'? – Angels Jul 04 '17 at 21:37
  • I have scoped them locally maioman with var in each loop. Thanks to you and Maioman. If there are better ways to define these I am open to this. – camdixon Jul 04 '17 at 21:37
  • @camdixon var scopes variable to the nearest function, let scopes it to the nearest block, in your situation 'for' block. Also syntax checkers prefer let and show warnings when var is used in such situations. – Angels Jul 04 '17 at 21:39

0 Answers0