Archived
📦 NEW: Ajout de la fonction findLongestWord
This commit is contained in:
1 parent
a2ee5e4f44
commit
4f6f448e40
3 files changed
+44
-2
No files matched your search
+2
-1
@@ -23,4 +23,5 @@ INSERT INTO `functions` (`id`, `title`, `slug`, `description`, `image`, `type`,
|
||||
(14, 'Juste Prix', 'rightPrice', 'Arriverez-vous à deviner le prix d\'un objet ?', '/images/functions/rightPrice.png', 'page', NULL, NULL, 1, '2020-04-27 20:17:05', '2020-04-27 21:59:22', 3),
|
||||
(15, 'Chronomètre', 'chronometerTimer', 'Gérer votre temps facilement (et adopter la technique Pomodoro).', '/images/functions/chronometerTimer.png', 'page', NULL, NULL, 1, '2020-04-29 09:28:08', '2020-04-29 09:45:29', 1),
|
||||
(16, 'Les méthodes des tableaux', 'arrayMethods', 'Et si on refaisait les méthodes natives de l\'objet global \"Array\" ?', '/images/functions/arrayMethods.png', 'article', '<p><strong>JavaScript</strong> est un langage <strong>orienté objet</strong>, chaque type comme \"String\", \"Number\", ... sont en réalité des objets. <br>\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\nCe sont des \"<a target=\"_blank\" href=\"https://developer.mozilla.org/fr/docs/Web/JavaScript/Reference/Objets_globaux\">Objets globaux</a>\", qui dispose de méthodes bien utiles. 😎 Pourquoi je vous dit ça ?<br>\n\n\n\n\n\n\n\n\n\n\n\n\n\n\nCar aujourd\"hui nous allons reproduire les méthodes de l\"objet global \"<a target=\"_blank\" href=\"https://developer.mozilla.org/fr/docs/Web/JavaScript/Reference/Objets_globaux/Array\">Array</a>\", c\"est un bon exercice pour progresser.<br>\n<br>\n\n\n\n\n\n\n\n\n\n\n\nNous allons réaliser une classe (syntaxe ES6+) qu\"on va appeler \"DivloArray\" par exemple et reproduire 6 méthodes (push, forEach, map, filter, reverse et join), bien évidemment libre à vous d\"en faire plus. 😊 Je vais expliquer le code une méthode après l\"autre donc bout de code par bout de code, si vous voulez avoir le code source complet, il est disponible sur ce <a target=\"_blank\" href=\"https://gist.github.com/Divlo/4ffc8ed007d293372ee449d2647fcad9\">GitHub Gist.</a><br>\n<br>\n\n\n\n\n\n\n\n\n\n\nDonc tout d\"abord, nous devons créer la classe avec un constructor et faire une nouvelle instance de cet objet dans une variable :<br>\n\n\n\n\n\n\n\n\n(Libre à vous de renommer les variables comme bon vous semble.) <br>\n</p><div class=\"vscode-editor\" style=\"color: #d4d4d4;background-color: #1e1e1e;font-family: Consolas, \'Courier New\', monospace;font-weight: normal;font-size: 14px;line-height: 19px;white-space: pre;\"><div><span style=\"color: #569cd6;\">class</span><span style=\"color: #d4d4d4;\"> </span><span style=\"color: #4ec9b0;\">DivloArray</span><span style=\"color: #d4d4d4;\"> {</span></div><div><span style=\"color: #d4d4d4;\"> </span><span style=\"color: #569cd6;\">constructor</span><span style=\"color: #d4d4d4;\">(</span><span style=\"color: #9cdcfe;\">array</span><span style=\"color: #d4d4d4;\">) {</span></div><div><span style=\"color: #d4d4d4;\"> </span><span style=\"color: #569cd6;\">this</span><span style=\"color: #d4d4d4;\">.</span><span style=\"color: #9cdcfe;\">array</span><span style=\"color: #d4d4d4;\"> = </span><span style=\"color: #9cdcfe;\">array</span><span style=\"color: #d4d4d4;\">;</span></div><div><span style=\"color: #d4d4d4;\"> }</span></div><div><span style=\"color: #d4d4d4;\">}</span></div><br>\n\n<div><span style=\"color: #569cd6;\">const</span><span style=\"color: #d4d4d4;\"> </span><span style=\"color: #9cdcfe;\">divloArray</span><span style=\"color: #d4d4d4;\"> = </span><span style=\"color: #569cd6;\">new</span><span style=\"color: #d4d4d4;\"> </span><span style=\"color: #4ec9b0;\">DivloArray</span><span style=\"color: #d4d4d4;\">([</span><span style=\"color: #b5cea8;\">57</span><span style=\"color: #d4d4d4;\">, </span><span style=\"color: #b5cea8;\">4</span><span style=\"color: #d4d4d4;\">, </span><span style=\"color: #b5cea8;\">2</span><span style=\"color: #d4d4d4;\">, </span><span style=\"color: #b5cea8;\">8</span><span style=\"color: #d4d4d4;\">, </span><span style=\"color: #b5cea8;\">16</span><span style=\"color: #d4d4d4;\">, </span><span style=\"color: #b5cea8;\">25</span><span style=\"color: #d4d4d4;\">]);</span></div></div><p><br>\n\n\n\n\n\n\n- Nous allons commencer par créer la première méthode très utilisée qui consiste à ajouter un ou plusieurs éléments à la fin d\"un tableau, je parle bien évidemment de la méthode <strong>push</strong>.</p><div class=\"vscode-editor\" style=\"color: #d4d4d4;background-color: #1e1e1e;font-family: Consolas, \'Courier New\', monospace;font-weight: normal;font-size: 14px;line-height: 19px;white-space: pre;\"><div><span style=\"color: #6a9955;\">/**</span></div><div><span style=\"color: #6a9955;\"> * </span><span style=\"color: #569cd6;\">@description</span><span style=\"color: #6a9955;\"> Ajoute un ou plusieurs éléments à la fin d\"un tableau et retourne la nouvelle taille du tableau.</span></div><div><span style=\"color: #6a9955;\"> * </span><span style=\"color: #569cd6;\">@param</span><span style=\"color: #6a9955;\"> </span><span style=\"color: #4ec9b0;\">{*}</span><span style=\"color: #6a9955;\"> </span><span style=\"color: #9cdcfe;\">elements</span><span style=\"color: #6a9955;\"> Éléments à ajoutés</span></div><div><span style=\"color: #6a9955;\"> * </span><span style=\"color: #569cd6;\Line truncated
|
||||
(17, 'Est-ce un palindrome ?', 'isPalindrome', 'Un palindrome est un mot (ou phrase) qui peut s\'écrire dans les deux sens (ex: kayak).', '/images/functions/isPalindrome.png', 'form', NULL, '[{\"name\": \"string\", \"type\": \"text\", \"label\": \"Entrez la chaîne de caractères à vérifier :\", \"placeholder\": \"(e.g : kayak)\"}]', 1, '2020-05-06 20:16:41', '2020-05-07 09:32:02', 2);
|
||||
(17, 'Est-ce un palindrome ?', 'isPalindrome', 'Un palindrome est un mot (ou phrase) qui peut s\'écrire dans les deux sens (ex: kayak).', '/images/functions/isPalindrome.png', 'form', NULL, '[{\"name\": \"string\", \"type\": \"text\", \"label\": \"Entrez la chaîne de caractères à vérifier :\", \"placeholder\": \"(e.g : kayak)\"}]', 1, '2020-05-06 20:16:41', '2020-05-07 09:32:02', 2),
|
||||
(18, 'Quel est le mot le plus long ?', 'findLongestWord', 'Trouve le mot le plus long dans une chaîne de caractères.', '/images/functions/findLongestWord.png', 'form', NULL, '[{\"name\": \"string\", \"type\": \"text\", \"label\": \"Entrez la chaîne de caractères (phrase) à vérifier :\", \"placeholder\": \"(e.g : Une phrase quelconque.)\"}]', 1, '2020-05-06 20:29:05', '2020-05-07 13:31:30', 2);
|
||||
@@ -12,6 +12,7 @@ const randomQuote = require('./main/randomQuote');
|
||||
const linkShortener = require('./main/linkShortener');
|
||||
const rightPriceOutput = require('./main/rightPrice');
|
||||
const isPalindromeOutput = require('./main/isPalindrome');
|
||||
const findLongestWordOutput = require('./main/findLongestWord');
|
||||
|
||||
const functionObject = {
|
||||
randomNumber : randomNumberOutput,
|
||||
@@ -27,7 +28,8 @@ const functionObject = {
|
||||
randomQuote : randomQuote,
|
||||
linkShortener : linkShortener,
|
||||
rightPrice : rightPriceOutput,
|
||||
isPalindrome : isPalindromeOutput
|
||||
isPalindrome : isPalindromeOutput,
|
||||
findLongestWord : findLongestWordOutput,
|
||||
};
|
||||
|
||||
// Choisi la fonction à exécuter
|
||||
|
||||
@@ -0,0 +1,39 @@
|
||||
const errorHandling = require('../../utils/errorHandling');
|
||||
const { requiredFields } = require('../../config/errors');
|
||||
|
||||
/**
|
||||
* @description Renvoie le mot le plus long d'une chaîne de caractères
|
||||
* @param {string} string
|
||||
* @returns {string}
|
||||
* @example findLongestWord('Chaîne de caractères') → 'caractères'
|
||||
*/
|
||||
function findLongestWord(string) {
|
||||
const arrayString = string.split(" ");
|
||||
let stringLength = 0;
|
||||
let result = "";
|
||||
|
||||
arrayString.forEach((element) => {
|
||||
if (element.length > stringLength) {
|
||||
result = element;
|
||||
stringLength = element.length;
|
||||
}
|
||||
});
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
/* OUTPUTS */
|
||||
module.exports = findLongestWordOutput = ({ res, next }, argsObject) => {
|
||||
const { string } = argsObject;
|
||||
|
||||
// S'il n'y a pas les champs obligatoire
|
||||
if (!(string)) {
|
||||
return errorHandling(next, requiredFields);
|
||||
}
|
||||
|
||||
const result = findLongestWord(string);
|
||||
return res.status(200).json({
|
||||
result,
|
||||
resultHTML: `<p>Le mot le plus long est : <br/>"${result}"</p>`
|
||||
});
|
||||
}
|
||||
Reference in new issue
Block a user