palindrome in javascript

Solutions on MaxInterview for palindrome in javascript by the best coders in the world

showing results for - "palindrome in javascript"
Lea
12 Mar 2018
1function palindrome(str) {
2
3    var len = str.length;
4    var mid = Math.floor(len/2);
5
6    for ( var i = 0; i < mid; i++ ) {
7        if (str[i] !== str[len - 1 - i]) {
8            return false;
9        }
10    }
11
12    return true;
13}
14
Kiefer
10 Nov 2017
1function palindrome(str) {
2 // Step 1. The first part is the same as earlier
3 var re = /[^A-Za-z0-9]/g; // or var re = /[\W_]/g;
4 str = str.toLowerCase().replace(re, '');
5
6 // Step 2. Create the FOR loop
7 var len = str.length; // var len = "A man, a plan, a canal. Panama".length = 30
8 
9 for (var i = 0; i < len/2; i++) {
10   if (str[i] !== str[len - 1 - i]) { // As long as the characters from each part match, the FOR loop will go on
11       return false; // When the characters don't match anymore, false is returned and we exit the FOR loop
12   }
13   /* Here len/2 = 15
14      For each iteration: i = ?    i < len/2    i++    if(str[i] !== str[len - 1 - i])?
15      1st iteration:        0        yes         1     if(str[0] !== str[15 - 1 - 0])? => if("a"  !==  "a")? // false
16      2nd iteration:        1        yes         2     if(str[1] !== str[15 - 1 - 1])? => if("m"  !==  "m")? // false      
17      3rd iteration:        2        yes         3     if(str[2] !== str[15 - 1 - 2])? => if("a"  !==  "a")? // false  
18      4th iteration:        3        yes         4     if(str[3] !== str[15 - 1 - 3])? => if("n"  !==  "n")? // false  
19      5th iteration:        4        yes         5     if(str[4] !== str[15 - 1 - 4])? => if("a"  !==  "a")? // false
20      6th iteration:        5        yes         6     if(str[5] !== str[15 - 1 - 5])? => if("p"  !==  "p")? // false
21      7th iteration:        6        yes         7     if(str[6] !== str[15 - 1 - 6])? => if("l"  !==  "l")? // false
22      8th iteration:        7        yes         8     if(str[7] !== str[15 - 1 - 7])? => if("a"  !==  "a")? // false
23      9th iteration:        8        yes         9     if(str[8] !== str[15 - 1 - 8])? => if("n"  !==  "n")? // false
24     10th iteration:        9        yes        10     if(str[9] !== str[15 - 1 - 9])? => if("a"  !==  "a")? // false
25     11th iteration:       10        yes        11    if(str[10] !== str[15 - 1 - 10])? => if("c" !==  "c")? // false
26     12th iteration:       11        yes        12    if(str[11] !== str[15 - 1 - 11])? => if("a" !==  "a")? // false
27     13th iteration:       12        yes        13    if(str[12] !== str[15 - 1 - 12])? => if("n" !==  "n")? // false
28     14th iteration:       13        yes        14    if(str[13] !== str[15 - 1 - 13])? => if("a" !==  "a")? // false
29     15th iteration:       14        yes        15    if(str[14] !== str[15 - 1 - 14])? => if("l" !==  "l")? // false
30     16th iteration:       15        no               
31    End of the FOR Loop*/
32 }
33 return true; // Both parts are strictly equal, it returns true => The string is a palindrome
34}
35
36palindrome("A man, a plan, a canal. Panama");
Marquis
28 Sep 2016
1function remove_spaces_strict(string)
2{
3    return string.replace(/\s/gm, "");
4}
5
6function to_lower_case(string="")
7{
8    return string.toLocaleLowerCase();
9}
10
11
12function isPalindrome(string) {
13    let string1 = string;
14    if (typeof string === 'string' || typeof string === 'number') {
15        if (typeof string === 'number') {
16            string1 = new Number(string1).toString();
17        }
18        string1 = remove_spaces_strict(string1);
19        string1 = to_lower_case(string1);
20        let len1 = string1.length;
21        let len = Math.floor(string1.length / 2);
22        let i = 0;
23
24        while(i < len) {
25    
26                if (string1[i] !== string1[len1 - (1 + i)]) {
27                    return false;
28                }
29                console.log(string1[i] + " = " + string1[len1 - (1 + i)]);
30                i++;
31            }
32    }else{
33        throw TypeError(`Was expecting an argument of type string1 or number, recieved an argument of type ${ typeof string1}`);
34    }
35    return string;
36}
37
queries leading to this page
palindrome string in jsis number a palindrome javascriptpalindrome program in javascriptto check if string is palindrome in javascript or notprogram to check if string is palindrome jspalindrome javascript questioncheck for palindrome javascriptpalindrome in javascript using for looppalindrome program in javascirptpalindrome in jspalindrome checker source code javascriptwrite a program for palindrome string in javascripthow to check for palindrome in javascriptlargest palindrome in string in jsjavascript palindrome programchecking palindrome with maphow to check for a palindrome in javascriptpalindrome program in jsis palindrome in jsjavascript function palindromepoliondrome function javascriptgiven the string check if it is a palindrome javascripthow to check a string is palindrome or not in javascriptreturn palindrom string in jspalindrome javascript functionjs palindrome stringgiven the string 2c check if it is a palindrome javascriptpalandrom in jscheck if a word is a palindrome javascriptjavascript check if palindromecheck palindrome in jspalindrome number in javascriptpalindrome function in javascriptcheck if a string is palindrome javascriptjs palindrome numberfind palindrome of anumber jaavscriptprint 1 if string is palindrome or not in javascriptcheck if string is palindrome javascriptjavascript is palindromehow to write function to check palindrome in jspalindrome again jspalindrome checker javascripthow to check if a string is a palindrome javascriptfunction is palindrome javascriptpalindrome javascriptjs check palindromestring palindrome in es6apalindrome string in jspalindrom for sentence in jscheck palindrome number in javascriptpalindrome number jsjs palindrome of a namepalindrome javascrppalindrome in javascript examplevalid palindrome javascriptcheck whether palindrome javascriptpalindrome function javascriptjs palindromes stringpalindrome number in javascript using functionjavascript palindromepalindrome jshow to find all numeric palindromes in javascriptnumber palindrome in javascriptalmost palindrome javascriptpalindrome or not javascriptpalindrome string jspalindrome javascript for looppalindrome example javascriptcode to check palindrome in javascripthow to write a function with a palidrome jsjs print palindromecheck palindrome javascriptalternative palindrome code in javascriptjavascript palindrome functionhow to check palindrome in javascriptjavascript palindrome checkerjs palindromepalindrome code in node jscheck if string is a palindrome javascripthow to check if string is pelindrome javascriptpalendrome finder javascriptcheck palindrome jshow to check if a number is a palindrome in javascriptpalindrome code jshow to check if a string is a palindrome in javascriptpalindromo jspalindrome checker jscheck for palindrome in javascriptjavascript a string is k palindrome if it becomes a palindrome on removing at most k characters from it js is palindrome functionjavascript code for palindromefunction for palindrome javascriptcheck palindrome in javascriptpalindrome string in javascript with a and b variablepalindrome number javascript palindrome in a string javascriptpalindrom checker jspalindrome algorithm javascriptjavascript function to check palindromehow to check given string is palindrome or not in jspalindrome pure javascriptjs palindrompalindrome challenge javascriptcheck palindrome function jspalindrome checker in javascriptpalindrome javascriptpalindrome coding question jspalindrome in javascriptjavascript program to check for palindromehow to write a function to check palindrome in jscheck whether string is palindrome or not jvascripthighest value palindrome javascriptis palindrome javascriptcheck for palindrome jsfunction to check palindrome in javascripthow to find palindromes in jspalindrome code in javascripthow to match a string palindrome in jspalindrome check jspalindrome code node jspalindrome number in javascript directlylongest palindrome javascriptpalindrome string program in javascriptwrite a program that find palindrom elements of array in jschecking for palindrome jspalindrome in javascript