-1

I try to code a TextAdeventure and I want that a value is added by one so the program can continue with the array of part2 , then part3 and so on.

The program is showing the first part (array1) correctly. Then the value is added by 1 and the program should show the second part (array2), but it is still showing part 1. The arrays are called "part1", "part2", and so on.

When I console.log the variable "partvalue", then it shows "part2", but nevertheless the function is working with "part1". Thanks for helping.

var iCounterText = 0;                           
var value = 1;
var partvalue = eval("part" + value);

var verzog = setInterval(function(){
    if (iCounterText < partvalue.length-3) {
        ++iCounterText;
        toggleText.insertAdjacentHTML('beforeBegin', '<br>--------------<br>' +   partvalue[iCounterText]);
        playaudio();
    }
    else {
        buttonLinks.innerHTML = partvalue[partvalue.length-1];
        ++value;
        iCounterText=0;
        buttonLinks.style.visibility = "visible";
        clearInterval(verzog);
    }
},500);
Joe Clay
  • 33,401
  • 4
  • 85
  • 85
Christian
  • 9
  • 4
  • 2
    Maybe it works if you do `iCounterText++;` and `value++;` instead :) – Asons Mar 11 '16 at 17:46
  • Building on @LGSon, check out http://stackoverflow.com/questions/6867876/javascript-i-vs-i – Joe Essey Mar 11 '16 at 17:54
  • do you mean, I should put the "++" behind the variables? Still the same then. – Christian Mar 11 '16 at 17:55
  • 2
    Why would the difference between `++var` and `var++` matter in this context? It's not being evaluated in the same expression that it's being incremented. – jered Mar 11 '16 at 17:55
  • It's a bit hard to decipher this code and figure out what it's trying to do. It would be helpful if the author could post a more abstract example. – jered Mar 11 '16 at 18:00
  • @Christian the only place I see `partvalue` being set is at the very top of your example. Is there somewhere else in the code that `partvalue` is being set? I will never hold a reference to "part2" unless you do so. You need another `partvalue = eval("part" + value)` somewhere, likely in the `else` block after you modify `value`. – jered Mar 11 '16 at 18:10
  • @Christian Updated my answer. Does it work with that? – Asons Mar 11 '16 at 18:45
  • @JDC , I guess you have the correct hint. partvalue is not automatically updating, when "value" has changed, so I have to put it in the else block,too. I was wrong, that console.log shows the array "part2". I guess I tried something different then. It is working now. Thank you all for your help. – Christian Mar 11 '16 at 21:47
  • Do not program this way. If you have a list of things, like part1, part2, and part3, manage them with an array. Do not construct variable names as strings and dereference them with eval. That may be a common programming idiom in PHP or some other languages, but in JS it is bad programming practice and won't even work in modern environments. –  Mar 12 '16 at 10:40
  • @LGSon I don't know if the updated answer works with my project. I have many Arrays, that contain about 10 strings and the arrays are named "part1", "part2", etc. Your version just shows the value of "value" and is not showing the content of the arrays or am I misundersanding something? I am new here, so there is a lot of stuff, I don't know yet. Is there a way I can name the arrays as numbers? Then I wouldn't need the eval stuff. – Christian Mar 12 '16 at 15:42
  • You can access an array like this `var parttext = partValue[value]` where `value` is your counter – Asons Mar 12 '16 at 15:56

1 Answers1

1

If you move the var partvalue = eval("part" + value); inside your setInterval function, I'm sure it will work as expected.

Updated

I now changed these 3 lines (and removed var partvalue = eval("part" + value);)

if (iCounterText < value) {
toggleText.insertAdjacentHTML('beforeBegin', '<br>--------------<br>part' + value);
buttonLinks.innerHTML = "part" + value;

So now it looks like this

var iCounterText = 0;                           
var value = 1;

var verzog = setInterval(function(){    
  if (iCounterText < value) {
    ++iCounterText;
    toggleText.insertAdjacentHTML('beforeBegin', '<br>--------------<br>part' + value);
    playaudio();
  }
  else {
    buttonLinks.innerHTML = "part" + value;
    ++value;
    iCounterText=0;
    buttonLinks.style.visibility = "visible";
    clearInterval(verzog);
  }
},500);
Asons
  • 84,923
  • 12
  • 110
  • 165