-1

I have an input type text where I letters but I want those letters to worth a number for example a=1 b=2 and I write ab in the input text and in a paragraph it appears 3

function add(){
}
<html>
<head>
</head>
<body>
<input type="text" placeholder="Texto" class="controls1" id="T1"> 
                <p class="controls" id="en1">Result</p>
                <input type="button" value="Result" class="R1" id="R1" onclick="add()">
</body>
</html>
Rom
  • 11
  • 3

1 Answers1

0

This should do it:

function add(){
        var stringArray = document.getElementById('T1').value;
        var answer = 0;

        for (var i = 0; i < stringArray.length; i++) {
            console.log(stringArray.charCodeAt(i) - 96);
            answer = answer + stringArray.charCodeAt(i) - 96;
        }

        document.getElementById('en1').innerHTML = answer;
    }
<div>
    <input type="text" placeholder="Texto" class="controls1" id="T1"> 
    <p class="controls" id="en1">Result</p>

    <input type="button" value="Result" class="R1" id="R1" onclick="add()">
</div>
Prosy Arceno
  • 2,616
  • 1
  • 8
  • 32