Question

In: Computer Science

Palindrome Javascript I am trying to write this javascript function to check if a number is...

Palindrome Javascript

I am trying to write this javascript function to check if a number is Palindrome..

Palindrome means - a word, phrase, or sequence that reads the same backward as forward, e.g., madam or nurses run.

This is the code i have, but it doesnt work.

Code:

let convertButton = document.getElementsByClassName("btn")[0];

let userInput = document.getElementById("number").value;

let results = document.getElementById("result").value;

convertButton.addEventListener("click", function (event) {

event.preventDefault();

console.log(userInput);

if (validatePalidrome(userInput))

document.getElementById("result").innerHTML = "true";

else document.getElementById("result").innerHTML = "false";

});

function validatePalidrome(numbers) {

let userStringArray = userInput.split("");

let reverseUserInput = userStringArray.reverse();

let joinReverseString = reverseUserInput.join("");

if (userInput.localeCompare(joinReverseString) === 0) {

return true;

}

return false;

}

Solutions

Expert Solution

According to the given code,

Your approach is effiecient and good but needs a slight modification.

Including that modification, I have written the palindrom function using JavaScript.

Below are the Snippets:

Code Snippet:

Output Snippet:

String to be passed: mam, returns true

String to be passed: hi, returns False

Code in Text format:

function validatePalidrome(numbers)                                       // getting inputString in numbers variable
{

   let userStringArray = numbers.split('');                           // converting string to array      
  
   let reverseUserInput = userStringArray.reverse();                   // reversing the elements in the array

   let joinReverseString = reverseUserInput.join('');                   // again joining them as a string

   if (numbers == joinReverseString)                                   // comparing the string with original string in numbers
   {

       return true;                                                   // returns true if it is a palindrome

   }
   else
   {

       return false;                                                   // returns false if it is not a palindrome

   }
}

I hope the above snippets, information, and the explanation will help you out!

Please comment if you have any queries/errors!

Thank you!


Related Solutions

Palindrome Javascript - NUMBERS I am trying to write this javascript function to check if a...
Palindrome Javascript - NUMBERS I am trying to write this javascript function to check if a number is Palindrome.. Palindrome means - a word, phrase, or sequence that reads the same backward as forward, e.g., 12321 This is the code i have, but it doesnt work. PLEASE MAKE SURE IT WORK BEFORE POST ANSWER, I GOT @ CODE THAT DID WORK BEFORE HTML JavaScript Palindrome Exercise rel="stylesheet" type="text/css" href="https://cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/4.4.1/css/bootstrap.min.css" /> Palindrome Enter a positive number: Is this a palindrome? No...
C# Palindrome Permutation: Given a string, write a function to check if it is a permutation...
C# Palindrome Permutation: Given a string, write a function to check if it is a permutation of a palindrome. A palindrome is a word or phrase that is the same forwards and backwards. A permutation is a rearrangement of letters. The palindrome does not need to be limited to just dictionary words. Input: Tact Coa Output: True (permutations: "taco cat", "atco cta", etc.) Comment your code to explain it.
I am trying to use the countif or countifs function to count the number of genres...
I am trying to use the countif or countifs function to count the number of genres used to describe a film and no matter what syntax I use, the result is 0. Can I use the countif(s) function to count columns? I want to count the number of movies that have 3 or more genre's associated with them. Here is a sample of the data I am working with: primarykey Movie Genre1 Genre2 Genre3 Genre4 Genre5 Genre6 Genre7 tt0499549 Avatar...
I am trying to implement a search function for a binary search tree. I am trying...
I am trying to implement a search function for a binary search tree. I am trying to get the output to print each element preceding the the target of the search. For example, in the code when I search for 19, the output should be "5-8-9-18-20-19" Please only modify the search function and also please walk me through what I did wrong. I am trying to figure this out. Here is my code: #include<iostream> using namespace std; class node {...
I wrote this program to check a string is palindrome or not. but in both cases,...
I wrote this program to check a string is palindrome or not. but in both cases, it gives me palindrome. could someone help me with this program? it has an error I couldn't find. when I run the code and give a string, in both cases it gives me palindrome. for example if I give Pop it says it is a palindrome but if I give Salima, it also says palindrome. #include<string> #include <iostream> using namespace std; class PString:public string...
I am trying to create a function in JAVA that takes in an ArrayList and sorts...
I am trying to create a function in JAVA that takes in an ArrayList and sorts the values by their distance in miles in increasing order. So the closest (or lowest) value would be first. It does not need to output the values in anyway, but it should return them so they can be output elsewhere. Please try to use the stub class below. The code for the main class is not necessary. I am only really asking for the...
I am trying to plot a difficult function in MATLAB, however I need to understand the...
I am trying to plot a difficult function in MATLAB, however I need to understand the basics first. I am trying to plot n=0 for x=0:0.01:2pi n=n+1 y(n)=sin(x) end I beleive what this says, is that I want to plot sin(x) over a full period hence from o to 2pi, and I beleive the 0.01 is the incremenation along the x-axis. I am not sure what my n is doing Could smeone please graph this for me with the MATLAB...
I am trying to write a code in C for a circuit board reads in a...
I am trying to write a code in C for a circuit board reads in a temperature from a sensor on the circuit board and reads it onto a 7-segment LED display. How exactly would you code a floating 2 or 3 digit reading onto the LED display? I am stuck on this part.
I am trying to write a java program that determines if an inputted year is a...
I am trying to write a java program that determines if an inputted year is a leap year or not, but I am not able to use if else statements how would I do it. I don't need the code just advice.
I am trying to write the code for an 8 bit adder in VHDL so that...
I am trying to write the code for an 8 bit adder in VHDL so that I can program it onto my Elbert V2 Spartan 3A FPGA Development Board, but I keep getting errors. Any ideas what I am doing wrong? library IEEE; use IEEE.STD_LOGIC_1164.ALL; entity adder8bit is Port ( a : in STD_LOGIC_VECTOR(7 downto 0); b : in STD_LOGIC_VECTOR(7 downto 0); cin : in STD_LOGIC; o : out STD_LOGIC_VECTOR(7 downto 0); cout : out STD_LOGIC); end adder8bit; architecture Behavioral...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT