Search
 
SCRIPT & CODE EXAMPLE
 

JAVASCRIPT

permutation javascript

const permutations = arr => {
  if (arr.length <= 2) return arr.length === 2 ? [arr, [arr[1], arr[0]]] : arr;
  return arr.reduce(
    (acc, item, i) =>
      acc.concat(
        permutations([...arr.slice(0, i), ...arr.slice(i + 1)]).map(val => [
          item,
          ...val,
        ])
      ),
    []
  );
};
Comment

array permutation

const permute = (input = [], permutation = []) => {
    if (input.length === 0) return [permutation]; // this will be one of the result

    // choose each number in a loop
    return input.reduce((allPermutations, current) => {
        // reduce the input by removing the current element
        // as we'll fix it by putting it in `permutation` array
        const rest = input.filter(n => n != current);
        return [
            ...allPermutations,
            // fixing our choice in the 2nd arg
            // by concatenationg current with permutation
            ...permute(rest, [...permutation, current])
        ];
    }, []);
}
Comment

PREVIOUS NEXT
Code Example
Javascript :: copy text to clipboard javascript without input 
Javascript :: express-generator 
Javascript :: how to make a vowel counter in javascript 
Javascript :: statusbar height react native 
Javascript :: delta time js 
Javascript :: node js to int 
Javascript :: jquery window redirect with header 
Javascript :: inject image javascript 
Javascript :: react bind function to component 
Javascript :: login page link shopify 
Javascript :: how to set cookies in node js 
Javascript :: jquery get td value 
Javascript :: how to add up all numbers in an array 
Javascript :: javascript return promise 
Javascript :: add array of object to state react 
Javascript :: how to use bootstrap icons in react components 
Javascript :: input field take only number and one comma 
Javascript :: javascript open new window with html content 
Javascript :: reverse a word javascript 
Javascript :: ReferenceError: primordials is not defined 
Javascript :: javascript minimum number in array 
Javascript :: email id domain check javascript 
Javascript :: jquery find children not working 
Javascript :: datepicker auto close 
Javascript :: boilerplate node js server 
Javascript :: javascript window size 
Javascript :: show hide more text jquery 
Javascript :: how to get a record in dynamodb nodejs 
Javascript :: Deep copy objects js JSON methods 
Javascript :: fetch 
ADD CONTENT
Topic
Content
Source link
Name
9+3 =