Skip to content

Commit ca47938

Browse files
authored
Add files via upload
1 parent 253d2f4 commit ca47938

File tree

1 file changed

+31
-0
lines changed

1 file changed

+31
-0
lines changed

JSprogram/Day4/Palindrome.js

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
//Palindrome or not
2+
let word=prompt("Enter word to check palindrome:").toLowerCase();
3+
let rev=""
4+
for(let i=word.length-1;i>=0;i--){
5+
rev+=word[i]
6+
}
7+
8+
rev===word ? console.log("its a palindrome"):console.log("its not a palindrome")
9+
10+
// Easiest version
11+
let isPalindrome = gon === gon.split("").reverse().join("");
12+
console.log(isPalindrome ? `${gon} is a palindrome`:`${gon} is not a palindrome`)
13+
14+
function longestPalindrome(s) {
15+
let longest = '';
16+
17+
for (let i = 0; i < s.length; i++) {
18+
for (let j = i; j < s.length; j++) {
19+
let substring = s.substring(i, j + 1);
20+
if (substring === substring.split('').reverse().join('') && substring.length > longest.length) {
21+
longest = substring;
22+
}
23+
}
24+
}
25+
26+
return longest;
27+
}
28+
console.log(longestPalindrome("babad")); // Output: "bab" or "aba" (both are valid)
29+
console.log(longestPalindrome("cbbcabc")); // Output: "cbbc"
30+
console.log(longestPalindrome("a")); // Output: "a"
31+
console.log(longestPalindrome("ac")); // Output: "a"

0 commit comments

Comments
 (0)