This repository has been archived on 2024-10-29. You can view files and clone it, but cannot push or open issues or pull requests.
FunctionProject/api/assets/functions/main/findLongestWord.js

40 lines
971 B
JavaScript
Raw Normal View History

2020-08-03 12:04:07 +02:00
const errorHandling = require('../../utils/errorHandling')
const { requiredFields } = require('../../config/errors')
/**
* @description Renvoie le mot le plus long d'une chaîne de caractères
2020-08-03 12:04:07 +02:00
* @param {string} string
* @returns {string}
* @example findLongestWord('Chaîne de caractères') 'caractères'
*/
2020-08-03 12:04:07 +02:00
function findLongestWord (string) {
const arrayString = string.split(' ')
let stringLength = 0
let result = ''
2020-08-03 14:14:45 +02:00
arrayString.forEach(element => {
2020-08-03 12:04:07 +02:00
if (element.length > stringLength) {
result = element
stringLength = element.length
}
})
2020-08-03 12:04:07 +02:00
return result
}
/* OUTPUTS */
2020-08-03 12:04:07 +02:00
module.exports = ({ res, next }, argsObject) => {
const { string } = argsObject
2020-08-03 12:04:07 +02:00
// S'il n'y a pas les champs obligatoire
2020-08-03 14:14:45 +02:00
if (!string) {
2020-08-03 12:04:07 +02:00
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>`
})
}