功能写出基地7字,像二进制计数器风格 [英] function for writing out a base 7 word, like binary counter style
问题描述
我需要类似的那些功能在这里解释...
<一个href=\"http://stackoverflow.com/questions/2328018/js-function-for-writing-out-a-word-binary-counter-style\">JS写出一个词功能,二进制计数器风格
...但使用基地7(或其他人)生成从A(计数)信达至G
像这样...
---一
--- b
- -C
--- D
---ê
- -F
- -G
--aa
--ab
--AC
- 广告
--ae
--af等多达GGGG
有一个简单的方法来改变这些功能之一来实现这一目标?
P.S。这是pretty很酷...结果
他们用...
VAR迭代= Math.pow(2,str.length)
和Math.pow(string.length减,2)
它们都工作,但仅仅是因为串长度为4,碱为2搜索
无意中正确
即4 ^ 2 = 16 2 ^ 4 = 16
如果使用其他任何字符串长度其中之一就错了。
即。 2 ^ 10 = 1024 10 ^ 2 = 100
函数change_num(NUM,基地){
VAR让='ABCDEFGHIJKLMNOPQRSTUVWXYZ';
让= lets.split('');
如果(NUM /碱基。1){
回归让[NUM]
}
如果(NUM /(Math.pow(基,2))≤; 1){
回归让[Math.floor(NUM /碱)] +让[(NUM%的基础);
}
如果(NUM /(Math.pow(基,3))。1){
VAR numreturn;
numreturn =让[Math.floor(NUM /(Math.pow(基,2)))];
numreturn + =让[Math.floor((NUM%Math.pow(基地,2))/基)];
numreturn + =让[(NUM%的基础);
返回numreturn;
}
如果(NUM /(Math.pow(基,4))。1){
VAR numreturn;
numreturn =让[Math.floor(NUM / Math.pow(基地,3));
numreturn + =让[Math.floor(NUM%Math.pow(基,3)/(Math.pow(基,2)))];
numreturn + =让[Math.floor((NUM%Math.pow(基地,2))/基)];
numreturn + =让[(NUM%的基础);
返回numreturn;
}
如果(NUM /(Math.pow(基,5))。1){
VAR numreturn;
numreturn =让[Math.floor(NUM / Math.pow(基地,4));
numreturn + =让[Math.floor(NUM%Math.pow(基地,4)/(Math.pow(基地,3)));
numreturn + =让[Math.floor(NUM%Math.pow(基,3)/(Math.pow(基,2)))];
numreturn + =让[Math.floor((NUM%Math.pow(基地,2))/基)];
numreturn + =让[(NUM%的基础);
返回numreturn;
}}
适用于任何基地。嫌麻烦不想写一个递归函数所以它没有硬盘的方式,你也许可以推断出我在做什么。
I need a function similar to the ones explained here...
JS function for writing out a word, binary counter style
...but using base 7 (or others) to generate (count) letters up from A to G Like so...
---a
---b
---c
---d
---e
---f
---g
--aa
--ab
--ac
--ad
--ae
--af
etc. up to gggg
Is there a simple way to change one of those functions to make this happen?
p.s. This is pretty cool...
They used...
var iterations = Math.pow(2,str.length)
and Math.pow(string.length,2)
They both work but only because the string length was 4 and the base was 2
inadvertently correct
i.e 4^2=16 2^4=16
If any other string length was used one of them becomes wrong.
i.e. 2^10=1024 10^2=100
function change_num(num,base) {
var lets = 'abcdefghijklmnopqrstuvwxyz';
lets = lets.split('');
if (num / base < 1) {
return lets[num];
}
if (num / (Math.pow(base,2)) < 1) {
return lets[Math.floor(num/base)] + lets[(num % base)];
}
if (num / (Math.pow(base,3)) < 1) {
var numreturn;
numreturn = lets[Math.floor(num/(Math.pow(base,2)))];
numreturn += lets[Math.floor((num%Math.pow(base,2))/base)];
numreturn += lets[(num % base)];
return numreturn;
}
if (num / (Math.pow(base,4)) < 1) {
var numreturn;
numreturn = lets[Math.floor(num/Math.pow(base,3))];
numreturn += lets[Math.floor(num%Math.pow(base,3)/(Math.pow(base,2)))];
numreturn += lets[Math.floor((num%Math.pow(base,2))/base)];
numreturn += lets[(num % base)];
return numreturn;
}
if (num / (Math.pow(base,5)) < 1) {
var numreturn;
numreturn = lets[Math.floor(num/Math.pow(base,4))];
numreturn += lets[Math.floor(num%Math.pow(base,4)/(Math.pow(base,3)))];
numreturn += lets[Math.floor(num%Math.pow(base,3)/(Math.pow(base,2)))];
numreturn += lets[Math.floor((num%Math.pow(base,2))/base)];
numreturn += lets[(num % base)];
return numreturn;
}
}
Works for any base. Couldn't be bothered to write a recursive function so did it the hard way, you can probably infer what I'm doing.
这篇关于功能写出基地7字,像二进制计数器风格的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!