-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathpalindrome.js
51 lines (41 loc) · 1.34 KB
/
palindrome.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
console.log("au moment ou le fichier est lu au premier chargement par le navigateur")
document.getElementById("recherchePalindrome").addEventListener("click", estCeUnPalindrome)
function estCeUnPalindrome () {
let mot = document.getElementById("mot").value.toLowerCase();
affiche( estCeQueLesMotsSontIdentiques( mot, renverseLe(mot) ))
}
// Fonctions d'affichage
function affiche(resultat) {
// affiche_le_resultat_avec_une_alert(resultat)
affiche_le_resultat_avec_un_innerHTML(resultat)
}
function affiche_le_resultat_avec_une_alert(resultat) {
alert(resultat)
}
function affiche_le_resultat_avec_un_innerHTML(resultat) {
document.getElementById("affichageResultat").innerHTML = resultat
}
// Fonctions de « renversage »
function renverseLe(mot) {
return renverse_detaille(mot)
// return renverse_court(mot)
}
function renverse_detaille(mot) {
let listeLettres = mot.split("")
let nouveauTableau = [];
for (let indice = listeLettres.length - 1; indice >= 0; indice--) {
nouveauTableau.push(listeLettres[indice])
}
return nouveauTableau.join("")
}
function reverse_court(mot) {
return mot.split("").reverse().join("");
}
// Fonction de comparaison
function estCeQueLesMotsSontIdentiques(mot, motInverse) {
if (mot === motInverse) {
return "Palindrome détecté !"
} else {
return "Ce mot n'est pas un palindrome"
}
}