Newer
Older
/**
* Return true if it's even, and false if it isn't.
*
* @param {Number} i
*/
export const isNumberEven = i => {
};
/**
* `str` is a string, but it may not have a file extension.
* Return the file extension (with no period) if it has one, otherwise false
* @param {String} str
*/
export const getFileExtension = str => {
let s =str.split(".");
if (s.length < 2){
return false;
}else{
return s.pop();
}
};
/**
* `arr`is a string.
* Return the longest string in the array
*
* @param {String} arr
*/
export const longestString = arr => {
return arr.reduce((precedent, courant) => {
if (typeof (courant) != "string"){
return precedent;
} else if (typeof (precedent) != "string") {
return courant;
} else if (courant.length < precedent.length){
return precedent;
}
return courant;
}
);
/**
* `str` is an string.
* Return a new string who's characters are in the opposite order to str's.
* @param {String} str
*/
export const reverseString = str => {
let oppositeOrder = "";
for (let i = str.length - 1; i >= 0; --i) {
oppositeOrder += str[i];
}
return oppositeOrder;
/**
* `str` is a string.
* Return true if it is a palindrome and false otherwise.
* It should be case insensitive and not consider space or punctuation.
*
* @param {String} str
*/
export const isPalindrome = str => {
// TODO
/**
* `arr` will be an array containing integers, strings and/or arrays like itself.
* Return the sum all the numbers you find, anywhere in the nest of arrays.
*/
export const nestedSum = arr => {
let sum = 0;
arr.forEach(e => {
if (Array.isArray(e)) {
sum += nestedSum(e);
}else if (typeof (e) == "number") {
sum += e;
}
});
return sum;
/**
* Retire du tableau `tab` passé en paramètre les éléments passés en
* paramètres dans `elms`.
*
* On utilise la destructuration pour pouvoir utiliser tous les arguments
* après `tab` comme un tableau.
*
* Après l'exécution le tableau d'origine est réellement modifié, ce
* on ne retourne pas une copie.
*
* Exemple :
* let tab = ['a', 'b', 'c', 'b', 'c'];
* pull(tab, 'a', 'b');
* tab; // ['c']
*
* @param {Array} tab
* @param {objects} elms
*/
export const retireDe = (tab, ...elms) => {
// TODO
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
/**
* Aplatit en profondeur un tableau passé en paramètre.
*
* Indications :
* - Utiliser la récursion.
* - Utiliser `Array.prototype.concat()` avec un tableau vide ([]) et l'opérateur de déstructuration (...) pour aplatir un tableau.
*
* Exemple :
* aplatirRecursif([5, [4], [[3], 2], [1], 0]);
* // [5, 4, 3, 2, 1, 0]
*/
export const aplatirRecursif = tab => {
//TODO
};
/**
* Retourne la liste de toutes les permutations des objets du tableau passé en paramètre.
*
* Exemple :
* permutations([0,1,2]);
* // [ [ 0, 1, 2 ],
* // [ 0, 2, 1 ],
* // [ 1, 0, 2 ],
* // [ 1, 2, 0 ],
* // [ 2, 0, 1 ],
* // [ 2, 1, 0 ] ]
*
* @param {Array} tab
*/
export const permutations = tab => {
if (tab.length <= 2) {
return tab.length === 2 ? [tab, [tab[1], tab[0]]] : tab;
}
return tab.reduce(
(acc, item, i) =>
acc.concat(
permutations([...tab.slice(0, i), ...tab.slice(i + 1)]).map(val => [
item,
...val,
])
),
[]
);
};
/**
* Retourne un élément au hazard parmi les éléments du tableau `tab` passé en
* paramètre.
*
* @param {Array} tab
*/
export const echantillon = tab => tab[Math.floor(Math.random() * tab.length)];
/**
* Prend un tableau 'tab' et le transforme en string avec chaque élément séparé par le `separateur`.
* Les deux derniers éléments sont séparé pas le séparateur `fin`.
*
* Exemple:
* enumerer(['Riri', 'Fifi', 'Loulou'], ', ', ' et ');
* // 'Riri, Fifi et Loulou'
*
*
* @param {Array} tab
* @param {string} separateur
* @param {string} fin
*/
export const enumerer = (tab, separateur = ', ', fin = separateur) => {
// TODO
/**
* Retourne, sous forme d'un tableau, les `n` plus grand nombres du tableau `tab` passé en paramètre.
*
* Attention, on ne doit pas modifier le tableau d'origine.
*
* Utiliser `Array.prototype.sort()`, l'opérateur de destructuration (...) et `Array.prototype.slice()`
*/
export const nMax = (tab, n = 1) => {
//TODO