Skip to main content
Inspiring
July 16, 2016
Answered

Javascript or advanced action help for incrementing variable names

  • July 16, 2016
  • 1 reply
  • 502 views

I hope someone with Javascript skill can help me here. And it might also be possible with advanced action but I'm unable to do it.

I have a variable "randomNumber" that chooses 1-128 on slide enter.

I have a series of variables - "but_word_1", "but_word_2" "but_word_3"  etc. all the way to "but_word_128".  These have text string values.

I have another variable "spoken_word"

On clicking a button, I'd like Javascript to set "spoken_word" with "but_word_" + "randomNumber"

ex. if randomNumber is 5 - then  window.cpAPIInterface.setVariableValue ("spoken_word", but_word_5);

Thanks.

    This topic has been closed for replies.
    Correct answer TLCMediaDesign

    I'm missing something here - I've tried both the advanced action and the javascript method, and where I have the text caption $$spoken_word$$ there appears the string "but_word_46" (for example) - but not the value for but_word_46.


    Try this:

    window.cpAPIInterface.setVariableValue ("spoken_word", window.cpAPIInterface.getVariableValue( "but_word_" + randomNumber ) );

    1 reply

    TLCMediaDesign
    Inspiring
    July 17, 2016

    window.cpAPIInterface.setVariableValue ("spoken_word", "but_word_" + Math.floor(Math.random() * 128) + 1 );

    Inspiring
    July 18, 2016

    Thanks - but I already have a variable for the random number-- called "randomNumber".  I'm using a separate script to create that (by shuffling an array so that i can exclude certain numbers)

    -In a different script I just want to append that randomNumber value to the string "but_word_" in order to identify the variable I'll assign to the spoken_word variable.  Sorry this is confusing- partly because my naming conventions are so inconsistent. I should have used camelBack the whole time- but I'm learning. Here's an example:

    If on slide-enter, I generate randomNumber = 46.

    I'd like to set the variable value of spoken_word with but_word_46.

    Lilybiri
    Legend
    July 18, 2016

    It can be done with a simple advanced action:

    Sorry for the translation to my labeling routine: v_btnRandom is your 'word', and v_random is the random number.