admin管理员组

文章数量:1316538

if anyone can give me some idea to begin with...at the end there.

i was looking input a word like e.g. "love" and get the sum of the numbers corresponding to each letter

answer = 54

var a = 1;var b = 2;var c = 3;var d = 4;var e = 5;var f = 6;var g = 7;
var h = 8;var i = 9;var j = 10;var k = 11;var l = 12;var m = 13;
var n = 14;var o = 15;var p = 16;var q = 17;var r = 18;var s = 19;var t = 20;
var u = 21;var v = 22;var w = 23;var x = 24;var y = 25;var z = 26;


var addLetters = new Array(1);
addLetters[1] = "love";
var square01 = 12 + 15 + 22 + 5 ;
function (){
document.getElementById(square01).innerHTML;}}

thanks to everyone for their help.

if anyone can give me some idea to begin with...at the end there.

i was looking input a word like e.g. "love" and get the sum of the numbers corresponding to each letter

answer = 54

var a = 1;var b = 2;var c = 3;var d = 4;var e = 5;var f = 6;var g = 7;
var h = 8;var i = 9;var j = 10;var k = 11;var l = 12;var m = 13;
var n = 14;var o = 15;var p = 16;var q = 17;var r = 18;var s = 19;var t = 20;
var u = 21;var v = 22;var w = 23;var x = 24;var y = 25;var z = 26;


var addLetters = new Array(1);
addLetters[1] = "love";
var square01 = 12 + 15 + 22 + 5 ;
function (){
document.getElementById(square01).innerHTML;}}

thanks to everyone for their help.

Share Improve this question edited Jan 19, 2012 at 6:27 Debby Dale asked Jan 18, 2012 at 17:31 Debby DaleDebby Dale 651 silver badge5 bronze badges
Add a ment  | 

7 Answers 7

Reset to default 4

You can use the ascii code of the characters in the string, which does not require the long array at all:

function sum(str) {
    var i, sum = 0, a = 'a'.charCodeAt(0) - 1;
    for (i = 0 ; i < str.length ; i++) {
        sum += str.charCodeAt(i) - a;
    }
}

alert(sum('love'));

You'll want to set up your letters like this:

var alphabet = {
    a: 1,
    b: 2,
    c: 3
}

var word = "love";
var total = 0;
for (var i = 0; i < word.length; i++)
    total += alphabet[word[i]];

DEMO


EDIT

@am not i am claims that IE8 won't index strings like that, and she's usually right, so, to be friendly to junk browsers you can do

for (var i = 0; i < word.length; i++)
    total += alphabet[word.charAt(i)];

instead of

for (var i = 0; i < word.length; i++)
    total += alphabet[word[i]];

You don't need to create the mapping array, assuming your word is all lowercase letters you can use:

    var word = 'love', total = 0, codeA='a'.charCodeAt();
    for ( var i = 0; i < word.length; i++ ) {
            total += word.charCodeAt( i ) - codeA + 1;
    }

charCodeAt() returns the Unicode value of a character, for the latin alphabet this is equal to its ASCII code which is sequential for letters

  1. Using closure with RegEx

    (function(w){
        var c=0;
        w.toLowerCase().replace(/[a-z]{1}/g,function(a){c+=a.charCodeAt(0)-97+1}); 
        return c;
    })("love")
    
  2. Trivial solution.

    var c=0;
    var str="love"
    var istr=str.toLowerCase()
    for(var i=0;i<istr.length;i++){
        c+=istr.charCodeAt(i)-"a".charCodeAt(0)+1
    }
    

Letters already have numerical assignments - ASCII value. A = 65, so just subtract the offset.

var phrase="abcd".toUpperCase();
var sum = 0
for(x=0;x<phrase.length;x++) {
     sum = sum + phrase.charCodeAt(x)-64;
}  
alert(sum)

This will work for what you want (notice the first element of alphabet is empty because we want a = 1, not 0)

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']
  , word = 'love'
  , letters = word.split('')
  , sum = 0

letters.forEach(function(letter){
  sum += alphabet.indexOf(letter)
})

Actually, It is not required to do all this stuff. I have a simple trick here....

var word="Love";
var total=0;
for(i=0;i<word.length;i++){
 total+=((word[i].toLowerCase()).charCodeAt(0)-96);     
 }
 alert(total); // aleters 54

本文标签: mathhow to return number for each letter in a string using javascriptStack Overflow