first commit with existing project files
This commit is contained in:
22
algo_js/palindrome.js
Normal file
22
algo_js/palindrome.js
Normal file
@@ -0,0 +1,22 @@
|
||||
const prompt = require('prompt-sync')();
|
||||
|
||||
// Palindromes
|
||||
|
||||
let word = prompt("Entrez une chaîne de caractères : ");
|
||||
|
||||
if (isPalindrome(word)) console.log(word + " est un palindrome.");
|
||||
else console.log(word + " n'est pas un palindrome.");
|
||||
|
||||
function isPalindrome(word) {
|
||||
|
||||
let left = 0;
|
||||
let right = word.length - 1;
|
||||
|
||||
for (let i = 1; i < word.length; i++) {
|
||||
//console.log("Left: " + word[left] + "(" + left + ")" + " Right: " + word[right] + "(" + right + ")");
|
||||
if (word[left] !== word[right]) return false;
|
||||
left++;
|
||||
right--;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
Reference in New Issue
Block a user