Exit
  • Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
  • 한국 커뮤니티
0

Passing values to Javascript arrays

Explorer ,
Jun 03, 2016 Jun 03, 2016

I'm trying to wrap my head around substituting and combining arrays in Javascript. I've found some code that works to combine arrays, de-duplicate and then sort alphabetically (below).  My question is, how can I fill these arrays with other values that are chosen by the user, rather than defining them myself in JS?  For instance, if a user clicks a different button, I'd like array1 to be filled with 4 new animal names?  I would think there's a way to assign a CSV string to the Captivate variable value and split this into array values in JS. Thanks.

Array.prototype.unique = function() {

    var a = this.concat();

    for(var i=0; i<a.length; ++i) {

        for(var j=i+1; j<a.length; ++j) {

            if(a === a)

                a.splice(j, 1);

        }

    }

    return a;

};

var array1 = ["cat","dog","snake","shark"];

var array2 = ["frog","cow","eagle","shark"];

var arrayFinal = array1.concat(array2).unique();

alert(arrayFinal.sort());

TOPICS
Advanced , Advanced actions
5.1K
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines

correct answers 1 Correct answer

People's Champ , Jun 05, 2016 Jun 05, 2016

If you put the following JavaScript in the head of your index.htm  just after the <script> tag an just above function initializeCP(), this code will work for you. If you select bedroom or any of the other arrays it will add it to the array, if you select it again it will remove it.

On the Library button execute JavaScript: newArray(library)

On the Bedroom button execute JavaScript: newArray(bedroom)

On the Kitchen button execute JavaScript: newArray(kitchen)

On the Submit button execute JavaScript:

...
Translate
People's Champ ,
Jun 03, 2016 Jun 03, 2016

Somehow I get the feeling that you are going about this the wrong way. If you are defining the arrays to begin with. Why don't you just set them up. Why concatenate when you defined them to begin with?

Most of these array functions are to manipulate dynamic data. In the above example, why don't you just create and array:

var arrayFinal = ["cat", "cow", "dog", "eagle", "frog", "snake","shark"];

Also, if you are having a hard time with arrays, I don't think you should be jumping into prototype.

Maybe you need a multi-dimensional array? Maybe if you would explain what you are trying to do we could set you down the right path.

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Jun 03, 2016 Jun 03, 2016

+1000 David, that is what I asked him already several times: what is the 'goal' he wants to achieve? Why store a CSV string in a Captivate variable to populate an array outside of Captivate to do what?

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Explorer ,
Jun 03, 2016 Jun 03, 2016

It's actually a pronunciation game.  A student will click on buttons with phonemes- like /p/ and then click another button like /b/. The program will then populate squares with words from each list of sounds. This leads into a game with audio in which students click the square they hear.  I want students to be able to choose which sounds they will draw words from, and those words will re-populate the squares. Does that make sense?

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
People's Champ ,
Jun 03, 2016 Jun 03, 2016

It does make some sense, but where do the arrays come in?

Are you wanting to set up a set of boxes and just change the text that is in the boxes?

If so, are you also going to need to change the associated audio that goes along with the group?

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Explorer ,
Jun 03, 2016 Jun 03, 2016

I can change the audio when the user clicks on the play button - using separate  advanced actions I can set up a conditional to match the word.  I think I have that part figured out.  But I'd like to draw words from different lists and then de-duplicate and alphabetize them.  Then I'll assign the words to variables so that each word appears on a different square.

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Explorer ,
Jun 03, 2016 Jun 03, 2016

Hi again, here I've made a pseudo project.  The variables "library", "bedroom" and "kitchen" have string values with four CSV items each:

chair,desk, book,shelf

bed,mirror,chair,shelf

shelf,counter,mirror,sink

Sorry if I'm asking the wrong questions.  This may be a lot more complicated than I imagine. If so, no problems - I'll ask my computer dept.

pseudo.jpg

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
People's Champ ,
Jun 05, 2016 Jun 05, 2016

If you put the following JavaScript in the head of your index.htm  just after the <script> tag an just above function initializeCP(), this code will work for you. If you select bedroom or any of the other arrays it will add it to the array, if you select it again it will remove it.

On the Library button execute JavaScript: newArray(library)

On the Bedroom button execute JavaScript: newArray(bedroom)

On the Kitchen button execute JavaScript: newArray(kitchen)

On the Submit button execute JavaScript: newThings( )

var someThings = [ ];
var selected = [ ];

var library = [ "chair", "desk", "book", "shelf" ];
var bedroom = [ "bed", "mirror", "chair", "shelf" ];
var kitchen = [ "shelf", "counter", "mirror", "sink" ];

function getUnique( arr )
{
    var dupes = [ ];

    return arr.filter( function( item )
{
        return dupes.hasOwnProperty( item ) ? false : ( dupes[ item ] = true );
    });
}

function newArray( arr )
{  
var temp = [ ];

if ( selected.indexOf( arr ) != -1 )

  selected.splice( selected.indexOf( arr ), 1 );
 
  for ( var i = 0; i < selected.length; i++ )
  {
   temp = temp.concat( selected[ i ] ).sort();
  }
 
  someThings = temp;
}
else
{
  selected[ selected.length ] = arr;
  someThings = someThings.concat( arr ).sort();
}
}

function newThings( )
{
var str = getUnique( someThings ).toString();
var res = str.replace( /,/g, ", ")
window.cpAPIInterface.setVariableValue( "things", res )
}

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Explorer ,
Jun 06, 2016 Jun 06, 2016
LATEST

Hi TLC Design,

I just want to thank you sooooo much for your help!! I had to tinker around and get help with inserting the code because I still have a lot to learn, but this will give me a lot to practice with in the weeks ahead.  I ended up following your instructions for the buttons, but put your code in the on enter slide javascript. There are a couple tweaks, which may make a difference in the function, so I'll paste the entire code below.  Again, wow, I'm incredibly grateful!

  • To complete the code that creates, filters, and sorts the array, a semicolon was added to the end of the line that contains ‘var res = str.replace( /,/g, ", ")’ and a closed bracket was added at the end of the ‘newThings( )’ function.

var someThings = [ ];

var selected = [ ];

var library = [ "chair", "desk", "book", "shelf" ];

var bedroom = [ "bed", "mirror", "chair", "shelf" ];

var kitchen = [ "shelf", "counter", "mirror", "sink" ];

function getUnique( arr )

{

    var dupes = [ ];

    return arr.filter( function( item )

{

        return dupes.hasOwnProperty( item ) ? false : ( dupes[ item ] = true );

    });

}

function newArray( arr )

{  

var temp = [ ];

if ( selected.indexOf( arr ) != -1 )

  selected.splice( selected.indexOf( arr ), 1 );

 

  for ( var i = 0; i < selected.length; i++ )

  {

   temp = temp.concat( selected[ i ] ).sort();

  }

 

  someThings = temp;

}

else

{

  selected[ selected.length ] = arr;

  someThings = someThings.concat( arr ).sort();

}

}

function newThings( )

{

var str = getUnique( someThings ).toString();

var res = str.replace( /,/g, ", ");

window.cpAPIInterface.setVariableValue( "things", res );

}

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Resources
Help resources