html - javascript to calculate the sum of Words - Stack Overflow

inside an html script i have inserted a src java script and created tables with id's which are lab

inside an html script i have inserted a src java script and created tables with id's which are labelled square1...e.g.<td id="square0">apple;</td>. i want the javascript to display the sum of the alphabets in the word which are in the html file. like if a is 1, b is 2, c is 3, d is 4...z is 26 then apple is 50, carrot is 75 and so on. i want the output to be like apple 50, carrot 75, yellow 92

the java script i tried was

var a = 1;
var b = 2;
...
...
var z = 26;
var addWord = new Array(1);
addWord[1] = "apple";
var square01 = 1 + 16 + 16 + 12 + 5;

function() {
    var total = 0;
    for (var i = 0; i < word.length; i++)
    total += alphabet[word[i]];
    document.getElementById(square0).innerHTML;
}
}

many thanks any help will do..

inside an html script i have inserted a src java script and created tables with id's which are labelled square1...e.g.<td id="square0">apple;</td>. i want the javascript to display the sum of the alphabets in the word which are in the html file. like if a is 1, b is 2, c is 3, d is 4...z is 26 then apple is 50, carrot is 75 and so on. i want the output to be like apple 50, carrot 75, yellow 92

the java script i tried was

var a = 1;
var b = 2;
...
...
var z = 26;
var addWord = new Array(1);
addWord[1] = "apple";
var square01 = 1 + 16 + 16 + 12 + 5;

function() {
    var total = 0;
    for (var i = 0; i < word.length; i++)
    total += alphabet[word[i]];
    document.getElementById(square0).innerHTML;
}
}

many thanks any help will do..

Share Improve this question edited Aug 2, 2012 at 17:57 underbar 5883 silver badges15 bronze badges asked Jul 29, 2012 at 8:56 Sarah BossSarah Boss 412 silver badges5 bronze badges 0
Add a ment  | 

5 Answers 5

Reset to default 3

Here is what you need.

var word = "Apple"
var sum = 0;
word.toUpperCase().split('').forEach(function(alphabet) {
    sum += alphabet.charCodeAt(0) - 64;
});
console.log(sum);

Explanation in puters characters are considered as number constants with a specific value like 65 - "A" , 66 - "B" , 97 - "a" , 98 - "b".

What we did here is split the word into characters after turning it into capital letters.

"A" , "P" , "P" , "L" , "E"

And then we found out the value of each character by the charCodeAt(0) call . and then we substract 64 from it so that we get the value of character as per your requirements

A - > 65- 64 = 1 B - > 66- 64 = 2

And so on.

You could calculate the sum of a word like this:

var word = "apple";
var sum = 0;
for (var i = 0; i < word.length; ++i) {
    sum += word.charAt(i).toLowerCase().charCodeAt(0) - 96;
}
//sum is 50

Use .charCodeAt() to get the ASCII value of the character. You can subtract 96 from the result to get a = 1, b = 2...

eg:

var word = "apple";
var sum = 0;

for(var i = 0; i < word.length; i++) {
    sum += (word[i].charCodeAt() - 96);
}
console.log(sum);​

JSFiddle

You'll have to do some checking if you want to handle upper case letters as well.

Abhishek's answer is more readable, but this is how to do it with reduce; it's slightly shorter but the javascript syntax makes it unclear that 0 is the initial value of sum:

'Apple'.toUpperCase().split('').reduce(
    function(sum,letter){ return sum + letter.charCodeAt(0)-64 },
    0
)

This works for letters "a".."z":

<!DOCTYPE html>
<html>
<head>
    <title></title>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
    <script type="text/javascript">
        function calcAlpha() {
            var word = document.getElementById("square0").childNodes[0].data;
            var sum = 0;
            for(var i=word.length-1; i>=0; i--) {
                sum += (word.charCodeAt(i) - 96);
            }
            document.getElementById("display").innerHTML=sum;
        }
    </script>
</head>
<body>
    <table><tr><td id="square0">apple</td></tr></table>
    <div id="display"></div>
    <button onclick="calcAlpha()">calculate</button>
</body>
</html>

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1744688863a4588085.html

相关推荐

  • html - javascript to calculate the sum of Words - Stack Overflow

    inside an html script i have inserted a src java script and created tables with id's which are lab

    11天前
    20

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信