Hello, I’ve completed the JavaScript palindrome checker project, I’ve run all of the tests successfully and when I click the “Run the Tests” button the tests run without any error.
However the project doesn’t complete… I’ve made some changes to the code and it still doesn’t complete the project.
Is the problem in the code or is it a bug of the course?
HTML:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<link rel="stylesheet" href="./styles.css">
<title>Palindrome checker</title>
</head>
<body>
<div>
<input id = "text-input"/>
<button id = "check-btn">check</button>
<div id = "result">
</div>
</div>
<script src="./script.js"></script>
</body>
</html>
JavaScript:
let text;
const inputValue = document.getElementById('text-input');
const checkButton = document.querySelector('#check-btn');
const result = document.querySelector("#result");
// initialize button
checkButton.onclick = checkInput;
//initialize input
inputValue.onchange = setText;
function checkInput(){
if(text === null || text === undefined || text.trim().length === 0){
alert("Please input a value");
} else {
removeNonAlphanumeric(text)
checkPalindrome(text);
}
}
function setText() {
text = inputValue.value;
}
function removeNonAlphanumeric(str) {
str = str.toLowerCase().replace(/[^a-zA-Z0-9]/g, '');
text = str;
};
function checkPalindrome(string) {
// convert string to an array
const arrayValues = string.split('');
// reverse the array values
const reverseArrayValues = arrayValues.reverse();
// convert array to string
const reverseString = reverseArrayValues.join('');
if(string == reverseString) {
result.innerText = inputValue.value + ' is a palindrome';
}
else {
result.innerText = inputValue.value + ' is not a palindrome';
}
}