Question

In: Computer Science

provide a JavaScript code that finds if the given word by user (prompt) is a Palindrome...

provide a JavaScript code that finds if the given word by user (prompt) is a Palindrome or no.

Solutions

Expert Solution

Please find the code , inline comments and output below.

<!DOCTYPE html>
<html>
<body>

<script>
function Palindrome_check(str){
// Change the string into lower case and remove all non-alphanumeric characters from the string
var new_str = str.toLowerCase().replace(/[^a-zA-Z0-9]+/g,'');
   var count = 0;
// Check whether the string is empty or not . If empty then return //false
   if(new_str==="") {
       return false;
   }
// Check if the length of the string is even or odd
   if ((new_str.length) % 2 === 0) {
       count = (new_str.length) / 2;
   } else {
// If the length of the string is 1 then it becomes a palindrome
       if (new_str.length === 1) {
           return true;
       } else {
// If the length of the string is odd ignore middle character
           count = (new_str.length - 1) / 2;
       }
   }
// Loop through to check the first character to the last character and then move next
   for (var i = 0; i < count; i++) {
// Compare characters and drop them if they do not match
       if (new_str[i] != new_str.slice(-1-i)[0]) {
           return false;
       }
   }
   return true;
}
var word = prompt("Enter the word : ", "");
var result =Palindrome_check(word);
if(result===true){
alert ("The given word is a palindrome");
}
else {
alert ("The given word is not a palindrome");
}

</script>

</body>
</html>

OUTPUT :


Related Solutions

Write Javascript code for the function malwareFrequencies() that shows the malware analyst a prompt. The analyst...
Write Javascript code for the function malwareFrequencies() that shows the malware analyst a prompt. The analyst enters a list of malware names (separated by spaces) for each malware incident they have heard about in the last month.  Note that the same malware can be involved in multiple incidents. Your function should print those malware names and their frequencies to the screen. Sample output is shown below. Zeus 1 Emotet 3 WannaCry 2 Emotet 3 Emotet 3 WannaCry 2
Please write a pep/9 assembly code that checks if a word or number is a palindrome
Please write a pep/9 assembly code that checks if a word or number is a palindrome
This code needs to be in C++, please. Step 1: Add code to prompt the user...
This code needs to be in C++, please. Step 1: Add code to prompt the user to enter the name of the room that they are entering information for. Validate the input of the name of the room so that an error is shown if the user does not enter a name for the room. The user must be given an unlimited amount of attempts to enter a name for the room. Step 2: Add Input Validation to the code...
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...
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...
Prompt the user for their name, get and store the user input. Prompt the user for...
Prompt the user for their name, get and store the user input. Prompt the user for their age, get and store the user input. We will assume that the user will enter a positive integer and will do no error checking for valid input. Determine and store a movie ticket price based on the user's age. If their age is 12 or under, the ticket price is $5. If their age is between 13 and 64, inclusive, the ticket price...
Reuse the code to prompt the user for a specific day, test whether it is within...
Reuse the code to prompt the user for a specific day, test whether it is within range, and then output the estimated concrete strength from the fitted curve using polyval. Run your function from the command window for 2 different days to test the functioning of your code. The program should preferably loop until the user asks to exit. where x = days y = rain (mm) x = [0 1 2 3 7 14 28]; y = [0 4...
There is error in this java code. Pls debug. // Prompt user for value to start...
There is error in this java code. Pls debug. // Prompt user for value to start // Value must be between 1 and 20 inclusive // At command line, count down to blastoff // With a brief pause between each displayed value import java.util.*; public class DebugSix3 { public static void main(String[] args) { Scanner input = new Scanner(System.in); String userNumString; int userNum, val; final int MIN = 1; final int MAX = 20; System.out.println("Enter a number between " +...
i need code in javascript or htmlt convert 0 to 999 numbers into word
i need code in javascript or htmlt convert 0 to 999 numbers into word
Prompt the user to enter an integer Then, prompt the user to enter a positive integer...
Prompt the user to enter an integer Then, prompt the user to enter a positive integer n2. Print out all the numbers that are entered after the last occurrence of n1 and whether each one is even or odd If n1 does not occur or there are no values after the last occurrence of n1, print out the message as indicated in the sample runs below. Sample: Enter n1: -2 Enter n2: 7 Enter 7 values: -2 3 3 -2...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT