jeudi 30 juin 2016

Javascript for Adobe Captivate

I got some help with some code to use in Captivate, and it works well. The idea is that when users click on any combination of 4 buttons and then submit, this will join the arrays, remove duplicates and then alphabetize the list in the variable called "things". What I've done is put word pairs (ex. "bet-bat") into the arrays, and it works well.

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

var b_or_v =["bet-vet","bat-vat","reb-rev","bick-vic","ban-van"];
var b_or_p =["back-pack","bet-pet","bit-pit","ban-pan","bin-pin"];
var I_or_e =["bit-bet","pit-pet","sit-set","rib-reb","pick-peck","pin-pen","rip-rep"];
var e_or_a =["bet-bat","set-sat","vet-vat","peck-pack","pet-pat","pen-pan","rep-rap"];
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+20;
  someThings = someThings.concat( arr ).sort();
} 
}
function newThings( )
{
var str = getUnique( someThings ).toString();
var res = str.replace( /,/g, ", ");
window.cpAPIInterface.setVariableValue( "things", res );
}

As you can see, I've used word pairs (ex. "bet-bat"). So now I want to erase the hyphens and break the word pairs into individual words in the list (alphabetized and no duplicates).

FOR EXAMPLE:

Right now if the user clicks "b_or_v" then b_or_p", the answer appears as: bet-vet,bat-van,reb-rev,bick-vic,ban-van,back-pack,bet-pet,bit-pit,ban-pan,bin-pin

But I want to split these word pairs, and de-duplicate them to get something like:

back, ban, bat, bet, bick, bin, pack, pan, pet, pin, reb, rev, van, vic

Any suggestions? Thanks. Bill

Aucun commentaire:

Enregistrer un commentaire