最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

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

programmeradmin1浏览0评论

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>
发布评论

评论列表(0)

  1. 暂无评论