I'm pretty new to JavaScript. I've written a function, which should take a String submitted to a form, and convert into array of chars, which then assigns each char a monetary value, and then returns the total of these values. The String may contain spaces, which should be given the value of zero. The following doesn't work though. I'm expecting it to display "Total number is: " plus the total on my html page. This function is called when information is entered into a text field, and a submit button hit. Instead, it just redirects to the page I have set under the action attribute in my tag.
Any pointers?
function validate() {
var total = 0;
var x = document.forms["myForm"]["UserInfo"].value;
var chars = x.toLowerCase().split('');
var alphabet = [" ", "a","b","c","d","e","f","g","h","i","j","k","l","m","n","o","p","q","r","s","t","u","v","w","x","y","z"];
for (i = 0; i < chars.length; i++) {
//var letterValue = alphabet.indexOf(splitted_string[i])+1;
var characterPosition = alphabet.indexOf(chars[i]);
total += characterPosition;
}
return total;
document.getElementById('error').innerHTML = "Total number is: " + total;
}
returning before affecting the DOM.returnstatement is above the line where you set an element'sinnerHTMLwith the answer.