JSFiddle - React, Tailwind, and code Playground

by frogggeee McFrogggeee

JavaScript

let input = "racecar";
palendrome(input);
/*
go through all letters in word.
reverse the word, and find the first letter that equals that letter.
select it and see if it matches the reversed version

banana
does anana == reversed(anana)
if so, palendrome

*/
function palendrome(word) {
	let i = 0;
  let palendromes = [];
  for (i = 0; i < word.length; i++) {
  	let vals = findValN(word.charAt(i), word);
    alert(word.substr(vals[0], vals[1]))
    for (let j = 1; j < vals.length-1; j++) {
    	if (word.substr(i, vals[j]) == rev(word.substr(i, vals[j])) && vals.length > 1) {
      	palendromes.push(word.substr(i, vals[j]));
      }
    }
  }
  alert(palendromes);
}
function findValN(val, word) {
	let result = [];
	for (let i = 0; i < word.length; i++) {
  	if (word.charAt(i) == val) {
    	result.push(i);
    }
  }
  return result;
}
function rev(word) {
	return word.split("").reverse().join("");
}