Tell us what’s happening:
There seems to be an error in one of the test cases. Specifically this one:
When the
#text-input
element contains the text0_0 (: /-\ :) 0-0
and the#check-btn
element is clicked, the#result
element should contain the text0_0 (: /-\ :) 0-0 is a palindrome
.
However, when manually copy-pasting the text, the output seems to match the desired result.
At several points in my code, I am outputting variables to the console for debugging purposes. When running the test, the output I am getting is as follows:
// running tests
When the #text-input element contains the text 0_0 (: /-\ :) 0-0 and the #check-btn element is clicked, the #result element should contain the text 0_0 (: /-\ :) 0-0 is a palindrome.
// tests completed
// console output
A
a
a
a
eye
eye
eye
eye
_eye
eye
eye
eye
race car
racecar
racecar
racecar
not a palindrome
notapalindrome
notapalindrome
emordnilapaton
A man, a plan, a canal. Panama
amanaplanacanalpanama
amanaplanacanalpanama
amanaplanacanalpanama
never odd or even
neveroddoreven
neveroddoreven
neveroddoreven
nope
nope
nope
epon
almostomla
almostomla
almostomla
almotsomla
My age is 0, 0 si ega ym.
myageis00siegaym
myageis00siegaym
myageis00siegaym
1 eye for of 1 eye.
1eyeforof1eye
1eyeforof1eye
eye1forofeye1
0_0 (: /- :) 0-0
00(:/:)00
00):\:(00
00(:\:)00
five|_/|four
five|/|four
five|\|four
ruof|\|evif
The first variable printed to the console is the raw input. The second input has characters removed (e.g. spaces and underscores), whereas the third one mirrors symbols (e.g. slashes and brackets). The final variable shows the input after it has been reversed.
It seems that the input for the test case is 0_0 (: /- :) 0-0
, rather than 0_0 (: /-\ :) 0-0
. If this is the case, then the test case is faulty. It will fail, even though it should pass, because the output string (the contents of #result
) contains the input string.
Your code so far
<!-- file: index.html -->
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>Palindrome Checker</title>
<link rel="stylesheet" href="style.css">
</head>
<body>
<form action="/action_page.php">
<label for="text-input">Palindrome:</label><br>
<input type="text" id="text-input" name="fname" value="John"><br>
<br>
<button type="button" onclick="palindroneChecker()" id="check-btn">Check</button>
</form>
<div id="result">
</div>
<script src="script.js"></script>
</body>
</html>
/* file: styles.css */
/* file: script.js */
function palindroneChecker() {
var rawInput = document.getElementById('text-input').value;
console.log(rawInput);
// input processing
var input = rawInput.toLowerCase(); // makes input lowercase
input = input.replaceAll(" ", ""); // removes spaces
input = input.replaceAll("_", ""); // removes underscores
input = input.replaceAll(".", ""); // removes periods
input = input.replaceAll(",", ""); // removes commas
input = input.replaceAll("-", ""); // removes hyphens
var array = input.split('');
var newArray = [];
for (var letter of array) {
if (letter == "/") { newArray.push("\\") }
else if (letter == "\\") { newArray.push("/") }
else if (letter == "(") { newArray.push(")") }
else if (letter == ")") { newArray.push("(") }
else if (letter == "{") { newArray.push("}") }
else if (letter == "}") { newArray.push("{") }
else { newArray.push(letter)}
}
var mirroredInput = newArray.join('');
console.log(input);
console.log(mirroredInput);
if (input == "") {
alert("Please input a value");
} else {
var reversedInput = "";
for (var i = input.length - 1; i >= 0; i--) {
reversedInput = reversedInput + mirroredInput[i];
}
console.log(reversedInput);
if (reversedInput === input) {
document.getElementById("result").innerHTML = rawInput + " is a palindrome";
} else {
document.getElementById("result").innerHTML = rawInput + " is not a palindrome";
}
}
}
Your browser information:
User Agent is: Mozilla/5.0 (Macintosh; Intel Mac OS X 10.15; rv:122.0) Gecko/20100101 Firefox/122.0
Challenge Information:
Build a Palindrome Checker Project - Build a Palindrome Checker