Sam Thompson Sam Thompson - 1 month ago 7
HTML Question

Javascipt function causing error not defined

I have a simple function that seems to be causing an 'Uncaught ReferenceError: arraySort is not defined' whenever the function is called, in this case by a button and i cant see why any help would be brilliant.

Javascipt

<script language="javascipt">
var unsorted = ["Printer","Tablet","Router"];
var alphaOrder = [" ","A","a","B","b","C","c","D","d","E","e","F","f","G","g", //15
"H","h","I","i","J","j","K","k","L","l","M","m","N","n","O", //30
"o","P","p","Q","q","R","r","S","s","T","t","U","u","V","v", //45
"W","w","X","x","Y","y","Z","z","0","1","2","3","4","5","6", //60
"7","8","9","'","?","!",".","\"","<",">","@",",","#","~","=", //75
"+","-","_","/","\\"];

function arraySort(array){
var sortedArray = [];
var letterNum = 0;
var numArray = [];

function letterToNum(){
for (var elementNum = 0; elementNum < array.length; elementNum++;){
for (var alphaNum = 0; alphaNum < alphaOrder.length; alphaNum++;){
numArray[elementNum] = alphaOrder.indexOf(array[elementNum][letterNum]);
document.getElementById('tester1').innerHTML = numArray;
}
}
}
}
</script>


HTML

<button type = "button" onclick = "arraySort(unsorted)">Sort</button>

Answer

Remove the semicolon from the end of your loops.

for (var elementNum = 0; elementNum < array.length; elementNum++) {
    for (var alphaNum = 0; alphaNum < alphaOrder.length; alphaNum++) {
    }