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