Add characters : Number Data Type « Language Basics « JavaScript DHTML






Add characters

 


<html>
<head>
<title>add characters test</title>

<script language="JavaScript">

function myCalc(){

  var myLetters = new Array(" ", "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");
  var myNumbers = new Array(0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 
                            16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26);
  var myPreAddends = document.myForm.myInput.value;
  var myLowerCase = myPreAddends.toLowerCase();
  var mySum = 0

  for(i=0; i<myLowerCase.length; i++) {
  myAddend = myLowerCase.charAt(i);
  for (x=0; x<myLetters.length; x++) {
    if (myAddend == myLetters[x]) {
    myAddend = myNumbers[x];
    }
  }
  mySum=mySum + myAddend
  
  }

  document.myForm.myResult.value = mySum;

}

function checkLength() {
  var InputCheck = document.myForm.myInput.value
  if (InputCheck.length > 15) {
  alert('Only 15 characters please!');

  }

}

</script>


</head>
<body>

<form name="myForm" onsubmit="myCalc(); return false;">
    <p>Please enter up to 15 letters (words, phrases, etc):
    
    <input name="myInput" onkeyup="checkLength()">

    <input type="submit" value="Calculate!" name="submit">
    <hr>
        If a=1, b=2, c=3, etc, then your letters add up to:<br>
    <input name="myResult">

</form>

</body>
</html>




           
         
  








Related examples in the same category

1.Define variables, assign values and output
2.Dividing by Zero
3.Add Four Numbers from an HTML Form (and Display the Results)
4.Complex class to represent complex numbers
5.A Function That Returns the Sum of Three Numbers (Stripped-Down Version)
6. Concatenating Variables and Displaying the Value Contained
7.JavaScript Loan Calculator
8.Factorials
9.Converting a Number to a String
10.Creating Number Objects Rather than Performing String-to-Number Conversions
11.Using toString() with Radix Values
12.Using JavaScript Integers
13.Conversion of Logical Values to Numeric Values
14.Using Floating-Point Numbers
15.Automatic Conversion between Types
16.Explicit Conversion Functions
17.Date Object Calculations
18.Operator Precedence and Different Data Types
19.Converting Base 10 to Base 16 Using Bitwise Operators
20.Number Calculation
21.Format a number
22.Format a number 2
23.Decimal to 2 Hex
24.Concatenate integer variable to a string variable