I've built a Palindrome checker in JavaScript. It's fairly simple.
I'm still learning JS and am looking to learn.
So I would love hear ideas on how to improve the JavaScript in this. Like more efficient ways to resolve the task of checking for Palindrome.
Source code is below. Link to CodePen is her: http://codepen.io/MarkBuskbjerg/pen/JWMWwN?editors=1010
HTML:
<div class="cover">
<div class="container header">
<div class="row row-margin">
<div class="col-sm-12">
<h1 class="text-center">Palindrome checker</h1>
</div>
<div class="col-sm-12">
<p class="text-center text-uppercase">A nut for a jar of tuna</p>
</div>
</div>
</div>
<div class="container">
<div class="row row-margin">
<div class="col-md-2">
</div>
<div class="col-md-8">
<textarea class="col-md-12" id="inputPalindrome" rows="5">Borrow or rob?</textarea>
</div>
</div>
<div class="row row-margin">
<div class="col-sm-3">
</div>
<button id="checkPalindrome" class="btn btn-default btn-lg btn-block col-sm-6 col-sm-offset-4" type="submit">Check palindrome</button>
</div>
</div>
<div class="container">
<div class="row row-margin">
<div class="col-md-2">
</div>
<div class="col-md-8 text-center">
<div id="notification" class="alert alert-info">Palindrome has not been checked yet</div>
</div>
</div>
</div>
CSS:
.cover {
height: 100vh;
}
.row-margin {
margin: 4vh auto;
}
body {
background: #FF4E50;
background: -webkit-linear-gradient(135deg, #FF4E50, #F9D423);
background: linear-gradient(135deg, #FF4E50, #F9D423);
}
.header {
color: #fff;
}
h1 {
font-family: "Pacifico";
}
.btn-default {
background-color: black;
color: #fff;
font-weight: bold;
text-transform: uppercase;
}
JavaScript:
var checkButton = document.getElementById("checkPalindrome");
function isPalindrome(str) {
str = str.toLowerCase().replace(/[^a-z0123456789]+/g,"");
var reversedStr = str.split("").reverse().join("");
if (str == reversedStr) {
return true
}
return false
}
checkButton.addEventListener("click", function() {
var palindromeInput = document.getElementById("inputPalindrome").value;
var palindromeReturn = isPalindrome(palindromeInput);
if(palindromeReturn === true) {
document.getElementById("notification").innerHTML = "Yay! You've got yourself a palindrome";
document.getElementById("notification").className = "alert alert-success";
} else {
document.getElementById("notification").innerHTML = "Nay! Ain't no palindrome";
document.getElementById("notification").className = "alert alert-danger";
}
});
Link to CodePen with the Palindrome Checker http://codepen.io/MarkBuskbjerg/pen/JWMWwN