Question

In: Computer Science

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

.JS

// Enter your code here

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

Note: Done accordingly. Please comment for any problem. Please Uprate. Thanks

Problems identified:

// Enter your code here

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

let userInput = document.getElementById("number").value; DONT KEEP THAT BEFORE FUNCTION CALL

let results = document.getElementById("result").value; AS IT WILL NO TAKE VALUES, IT IS LOADED WITH PAGE

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(""); YOU SHOULD HAVE USED numbers not userInput as your parameter is numbers

let reverseUserInput = userStringArray.reverse();

let joinReverseString = reverseUserInput.join("");

if (userInput.localeCompare(joinReverseString) === 0) { YOU SHOULD HAVE USED numbers not userInput as your parameter is numbers

return true;

}

return false;

}

Solution:

Html file:

<!DOCTYPE html>
<html lang="en">
<head>
<title>Bootstrap Example</title>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.4.1/css/bootstrap.min.css">
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.5.1/jquery.min.js"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.4.1/js/bootstrap.min.js"></script>
</head>
<body>
<div class="container">
<h2>Palindrome</h2>
<div class="form-group">
<label for="number">Enter a positive number:</label>
<input type="text" class="form-control" id="number">
</div>
<button type="button" class="btn">Check</button>
<div class="form-group">
Is this a palindrome?
<p id="result"></p>
</div>
</div>
</body>
<script src="palin.js">
</script>

</html>

Javascript file (palin.js):

let convertButton = document.getElementsByClassName("btn")[0];
convertButton.addEventListener("click", function (event) {

event.preventDefault();

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

console.log(userInput);

if (validatePalidrome(userInput))

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

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

});

function validatePalidrome(numbers) {

let userStringArray = numbers.split("");

let reverseUserInput = userStringArray.reverse();

let joinReverseString = reverseUserInput.join("");

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

return true;

}

return false;

}

Output:


Related Solutions

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...
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 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 {...
JavaScript Write out a function that takes in array of numbers. You do not need to...
JavaScript Write out a function that takes in array of numbers. You do not need to check if each item of the array is a number, assume this has already been done. Create a new array using the map function that that the original item and adds 4 to it. Then iterate through the new array and log each item to the console. If you do not use map it will be counted wrong. Call your function with the following...
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.
6.15 LAB: JavaScript arrays Write the function divideArray() in script.js that has a single numbers parameter...
6.15 LAB: JavaScript arrays Write the function divideArray() in script.js that has a single numbers parameter containing an array of integers. The function should divide numbers into two arrays, evenNums for even numbers and oddNums for odd numbers. Then the function should sort the two arrays and output the array values to the console. Ex: The function call: var nums = [4, 2, 9, 1, 8]; divideArray(nums); produces the console output: Even numbers: 2 4 8 Odd numbers: 1 9...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT