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 => {
str = str.toLowerCase();
let j = str.length - 1;
for (let i = 0; i < str.length && j > i; ++i) {
if (str[i] != ' ') {
while (str[j] == ' ') {
--j;
}
if (str[j] != str[i]) {
//is not a palindrome
return false;
}
--j;
}
}
//is a palindrome
return true;
/**
* `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) => {
elms.forEach(elm => {
while (tab.indexOf(elm) >= 0) {
tab.splice(tab.indexOf(elm), 1);
}
});
/**
* 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 => {
let t = [];
for (let i = 0; i < tab.length; ++i) {
if (Array.isArray(tab[i])) {
t.push(...aplatirRecursif(tab[i]));
} else {
t.push(tab[i]);
}
}
return t;
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
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
};
/**
* 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