Search
 
SCRIPT & CODE EXAMPLE
 
CODE EXAMPLE FOR JAVASCRIPT

Finding palindrome using for loop

let word = "CivIc";
let str = word.toLowerCase();

function checkPalindrome(str) {
  // find the length of the string
  const len = str.length;
  // loop through half of the string
  for (let i = 0; i < len / 2; i++) {
    // check each position
    // between the first and the last character
    if (str[i] !== str[len - 1 - i]) {
      console.log("NOT a palindrome");
    }
  }
  console.log("The string is a palindrome");
}
Source by sebhastian.com #
 
PREVIOUS NEXT
Tagged: #Finding #palindrome #loop
ADD COMMENT
Topic
Name
9+1 =