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

javascript - Assign a letter a numerical value in JS? - Stack Overflow

programmeradmin2浏览0评论

Is it possible to assign a certain amount of letters a numerical value, that can later be added together?

Ex:

A,B,C = 1
D,E,F = 2

So that if I add A + D + D I get 5?

I would post what I tried, but I don't even know what I could try in this sitation.

Is it possible to assign a certain amount of letters a numerical value, that can later be added together?

Ex:

A,B,C = 1
D,E,F = 2

So that if I add A + D + D I get 5?

I would post what I tried, but I don't even know what I could try in this sitation.

Share Improve this question asked Sep 3, 2012 at 4:19 CharlieCharlie 11.8k19 gold badges87 silver badges140 bronze badges
Add a ment  | 

3 Answers 3

Reset to default 6

You need to do

A = B = C = 1;
D = E = F = 2;

You could use a property map to assign the values:

var map = { A:1, B:1, C:1, D:2, E:2, F:2 };
console.log(map.A+map.D+map.D);

5

var alphabet = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ',
    letters = alphabet.split(''),
    len = letters.length,
    i;

for (i = 0; i < len; i++) {
    window[letters[i]] = Math.ceil((i + 1) / 3);
}

alert(D + D + A);

http://jsfiddle/zerkms/dKQS2/

发布评论

评论列表(0)

  1. 暂无评论