Commits (7)
......@@ -28,7 +28,17 @@ export const getFileExtension = str => {
* @param {String} arr
*/
export const longestString = arr => {
// TODO
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;
}
);
};
/**
......@@ -37,7 +47,12 @@ export const longestString = arr => {
* @param {String} str
*/
export const reverseString = str => {
// TODO
let oppositeOrder = "";
for (let i = str.length - 1; i >= 0; --i) {
oppositeOrder += str[i];
}
return oppositeOrder;
};
/**
......@@ -48,7 +63,22 @@ export const reverseString = str => {
* @param {String} str
*/
export const isPalindrome = str => {
// TODO
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;
};
/**
......@@ -56,7 +86,15 @@ export const isPalindrome = str => {
* Return the sum all the numbers you find, anywhere in the nest of arrays.
*/
export const nestedSum = arr => {
// TODO
let sum = 0;
arr.forEach(e => {
if (Array.isArray(e)) {
sum += nestedSum(e);
}else if (typeof (e) == "number") {
sum += e;
}
});
return sum;
};
/**
......@@ -78,7 +116,11 @@ export const nestedSum = arr => {
* @param {objects} elms
*/
export const retireDe = (tab, ...elms) => {
// TODO
elms.forEach(elm => {
while (tab.indexOf(elm) >= 0) {
tab.splice(tab.indexOf(elm), 1);
}
});
};
/**
......@@ -93,7 +135,15 @@ export const retireDe = (tab, ...elms) => {
* // [5, 4, 3, 2, 1, 0]
*/
export const aplatirRecursif = tab => {
//TODO
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;
};
/**
......@@ -148,7 +198,16 @@ export const echantillon = tab => tab[Math.floor(Math.random() * tab.length)];
* @param {string} fin
*/
export const enumerer = (tab, separateur = ', ', fin = separateur) => {
// TODO
let s = "";
for (let i = 0; i < tab.length; ++i) {
s += tab[i];
if (i < tab.length - 2) {
s += separateur;
} else if (i == tab.length - 2) {
s += fin;
}
}
return s;
};
/**
......