Home Ask Login Register

Developers Planet

Your answer is one click away!

Johanna Cristine Dy February 2016

how to convert this php function to jquery function. It returns every possible combination of the values from an array.

I found this PHP code from here:

function everyCombination($array) {
  $arrayCount      = count($array);
  $maxCombinations = pow($arrayCount, $arrayCount);
  $returnArray     = array();
  $conversionArray = array();
  foreach ($array as $key => $value) {
    $conversionArray[base_convert($key, 10, $arrayCount)] = $value;
  }
  for ($i = 0; $i < $maxCombinations; $i++) {
    $combination = base_convert($i, 10, $arrayCount);
    $combination = str_pad($combination, $arrayCount, "0", STR_PAD_LEFT);
    $returnArray[] = strtr($combination, $conversionArray);
  }
  return $returnArray;
}

print_r(everyCombination(array('a', 'b', 'c', 'd')));

which is what I need. But I need it to be in jquery so I tried to convert the code to jquery.

This is how I did it.

function everyCombination(arr)
{   //http://stackoverflow.com/questions/14022215/php-how-do-you-generate-all-possible-combinations-of-values-in-an-array
      var arrCount      = arr.length;
      var maxCombinations = Math.pow(arrCount, arrCount);
      var returnArr     = [];
      var conversionArr = [];

    $.each(arr, function( key, value )
    {

        conversionArr[base_convert(key, 10, arrCount)] = value;
    }); 
      for (i = 0; i < maxCombinations; i++)
      {
        var combination = base_convert(i, 10, arrCount);
       //var combination = str_pad(combination, arrCount, "0", STR_PAD_LEFT);
        var combination = str_pad(arrCount,"0",combination,true);
        var returnArr = strtr(combination, conversionArr);
      }
      return returnArr;
}

I tried to alert the data returned by everyCombination() function like this:

alert(everyCombination(['a', 'b', 'c', 'd'])); 

but i get only

dddd

instead of an array composed of;

Answers


Johanna Cristine Dy February 2016

I forgot to declare returnArr as array so it's value is being changed each time the for loop loops.

I fixed it by declaring it as an array and for each loop, i push the value to the array.

function everyCombination(arr)
{   //http://stackoverflow.com/questions/14022215/php-how-do-you-generate-all-possible-combinations-of-values-in-an-array
      var arrCount      = arr.length;
      var maxCombinations = Math.pow(arrCount, arrCount);
      var returnArr     = [];
      var conversionArr = [];
        var returnArr = [];

    $.each(arr, function( key, value )
    {

        conversionArr[base_convert(key, 10, arrCount)] = value;


    }); 
      for (i = 0; i < maxCombinations; i++)
      {
        var combination = base_convert(i, 10, arrCount);
       //var combination = str_pad(combination, arrCount, "0", STR_PAD_LEFT);
        var combination = str_pad(arrCount,"0",combination,true);
         returnArr.push(strtr(combination, conversionArr));
      }
      return returnArr;
}


budiantoip February 2016

It's because you are using a string variable, and the last value is stored. You need to use array variable to achieve your goals.
Try this (the global functions are put inside the global.js file) :

<!doctype html>
<html>
  <head>
    <title>http://stackoverflow.com/questions/35282110/jquery-function-which-returns-every-possible-combination-from-array-convert-php</title>
    <script src="//code.jquery.com/jquery-1.10.2.js"></script>
    <script src="global.js"></script>
  </head>
  <body>
    <script>
        function everyCombination(arr)
        {   //http://stackoverflow.com/questions/14022215/php-how-do-you-generate-all-possible-combinations-of-values-in-an-array
              var arrCount      = arr.length;
              var maxCombinations = Math.pow(arrCount, arrCount);
              var returnArr     = [];
              var conversionArr = [];

            $.each(arr, function( key, value )
            {

                conversionArr[base_convert(key, 10, arrCount)] = value;
            }); 
            var returnArr = [];
            for (i = 0; i < maxCombinations; i++)
            {
              var combination = base_convert(i, 10, arrCount);
             //var combination = str_pad(combination, arrCount, "0", STR_PAD_LEFT);
              var combination = str_pad(arrCount,"0",combination,true);
              returnArr.push(strtr(combination, conversionArr));
            }
            return returnArr;
        }
      (function(){
        alert(everyCombination(['a', 'b', 'c', 'd']));
      })();

    </script>
  </body>
</html>

Post Status

Asked in February 2016
Viewed 1,876 times
Voted 7
Answered 2 times

Search




Leave an answer


Quote of the day: live life