I have a pretty straightforward palindrome word test scriiiipt which seems to work perfect
http://fixmysite.us/Web_Programming_With_Javascriipt/Assignment6/
Heres the script im using
//prevent the page from resetting when the form is submitted
document.getElementById("palindome").addEventListener("submit", function(event){
event.preventDefault();
});
function checkInput() {
var word = [];
var input = document.getElementById('input').value;
//take out any spaces on left or right side of input
input = input.trim();
if(input.length > 10) {
alert('Your word cannot be longer than 10 characters');
location.reload();
} else {
//create an array & put each caracter into it
var wordArray = input.split("");
//call function to creater new []
var revArray = reverseArr(wordArray);
//make the new [] into a word
var solution = revArray.join();
//remove all ,s using regular expressions
solution = solution.replace(/\,/g,"");
//see if the word is a palindome
if(input == solution)
document.getElementById('result').innerHTML = '<p><img src="images/winner.png"></p><h2 id="winner">The word ('+input+') is a palindome.</h2>';
else
document.getElementById('result').innerHTML = '<p><img src="images/loser.png"></p><h2 id="loser">The word ('+input+') is NOT a palindrome</h2>';
}
}
function reverseArr(input) {
var ret = new Array;
for(var i = input.length-1; i >= 0; i--) {
ret.push(input[i]);
}
return ret;
}
The assitgnment was to make sure you use a callback function and am sort of confused as to what that is.
It seems like its simply a function that runs only when needed,
I though the preventDefault() would be one as its only run when the form is submitted. But just to be sure I want to make my call to the reverseArr function a callback
I thought I dso that like
var revArray = (function(wordArray) {
var ret = [];
for(var i = wordArray.length - 1; i >=0; i--)
ret.push(wordArray[i];
}
return ret;
});
is that right?