1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49
| <script>
"use strict";
/*
* Produit cartésien de n array
* Daniel Hagnoul v1.1.0 2015-10-10
*/
let produitCartesien = ( ts ) => {
const
tsL = ts.length,
tr = ( t1, t2 ) => {
let t3 = [];
for ( let elemI of t1 ) {
for ( let elemJ of t2 ) {
t3.push( elemI + '-' + elemJ );
}
}
return t3;
};
if ( tsL < 2 )
throw `Erreur dans produitCartesien, ts.length ( ${ tsL } ) < 2`;
let r = tr( ts[0], ts[1] );
for ( let n = 2; n < tsL; n++ ){
r = tr( r, ts[n] );
}
return {
"length" : r.length,
"arrayValue" : r,
"stringValue" : r.join()
};
};
let
ts = [
[ 'A', 'R', 'D', 'V', '10', '9', '8', '7', '6', '5', '4', '3', '2' ],
[ 'pique', 'c?ur', 'carreau', 'trèfle'],
[ 'a', 'b', 'c', 'd', 'e'],
[ '01', '02', '03', '04', '05']
],
pc = produitCartesien( ts );
console.log( pc.length, pc.arrayValue, pc.stringValue );
</script> |
Partager