0

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;  

} 
7
  • Can you be more specific when you say that the code doesn't work - what output are you getting? What are you expecting or wanting? Commented Jun 24, 2016 at 1:57
  • I'll update Daniel Commented Jun 24, 2016 at 1:58
  • 2
    You are returning before affecting the DOM. Commented Jun 24, 2016 at 2:00
  • 1
    Also, it looks like your return statement is above the line where you set an element's innerHTML with the answer. Commented Jun 24, 2016 at 2:00
  • Ah yes!! So simple!! Thank you so much both!! Commented Jun 24, 2016 at 2:02

2 Answers 2

4

Your return statement is above the line where you set an element's innerHTML with the answer. So, that statement is not getting executed.

Sign up to request clarification or add additional context in comments.

Comments

1

Worked for me

validate()
function validate() {

  var total = 0;

  var x = 'a sd'//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]);
    console.log(characterPosition);
    total += characterPosition;
  }
  console.log('total: ' + total)
  return total;

  document.getElementById('error').innerHTML = "Total number is: " + total;

}

Here my output

1

0

19

4

total: 24

My hunch is that your return statement is before your

`document.getElementById('error').innerHTML = "Total number is: " + total;`

so it doesn't get executed

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.