admin管理员组文章数量:1316693
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 badges7 Answers
Reset to default 4You 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
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")
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
版权声明:本文标题:math - how to return number for each letter in a string using javascript - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1742006604a2412082.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论