-3

I have managed to get so far but for some reason, it does not match the last number and run the else command I have in the code.

My current code is: JavaScript Fiddle

If anyone could point me in the right direction it would be very much appreciated as I've spent the last 2 hours trying to figure this problem out and only managed to get this far.

The next and Previous works fine, along with the View more/less. Its the 'View next address' that the code seems to skip the else.

This code here is always skipped, my question is does anyone know why or can point me in the direction of fixing this?

else {
  $(".activee").eq(idx).addClass("hidden");
  $(".activee").eq(idx).removeClass("activee").prev().addClass("activee");
  alert("Sorry, there is no more addresses to show...");
  num[idx]--;
}

Thanks in advance!

Jaquarh
  • 6,493
  • 7
  • 34
  • 86

2 Answers2

2

Change your fiddle to this:

var num = []; // <-- changed to array from object
for (i = 0; i < leng; i++) { // <-- while i < leng
  num.push(i);
}

Updated fiddle

StudioTime
  • 22,603
  • 38
  • 120
  • 207
  • 1
    That didn't even come up as an error in the Console window, never thought to check that! Thank-you so much! – Jaquarh Feb 24 '16 at 11:17
-2
 for (i = 0; i == leng; i++) {
    num.push(0);
  }

you're pushing the value 0 to the num array

probably meant to do

 for (i = 0; i == leng; i++) {
    num.push(howMany[i]);
  }

but I just skimmed the code, so am not sure whether the logic is even valid.

Pedro Costa
  • 2,968
  • 2
  • 18
  • 30
  • No, 0 is important to have otherwise the statement: `if(howMany[idx] != num[idx]){} else {}` would always return false but thanks – Jaquarh Feb 24 '16 at 11:12
  • exactly, wasn't your question "Why is it skipping my else statement?" because that statement will always be true... unless you can guarantee that howMany will at least have a 0 element. – Pedro Costa Feb 24 '16 at 11:20
  • But I can because `howMany` is an array of numbers, then each time the button next is pressed, `num` is incremented. The reason why it was always `true` is because I created an `Object` rather than array in `num`. – Jaquarh Feb 24 '16 at 11:23