2

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   
Community
  • 1
  • 1
gravityboy
  • 817
  • 5
  • 11
  • 21
  • That's not base 7, that's base 8 without a 0. – Ignacio Vazquez-Abrams Nov 25 '10 at 03:04
  • I actually was trying to figure what base to call it so I thought of an easy example... base 2 would be letters a and b. Using the same reasoning... 7 letters abcdefg would be base 7 – gravityboy Nov 25 '10 at 03:19
  • 1
    Except you also have `-`, making it 8 – Ignacio Vazquez-Abrams Nov 25 '10 at 03:42
  • hmmm... No, I thought about this again. In the linked page they used word length 4 abcd and the - hyphen... it is still base two, NOT 5. The number of letters in my word abcdefg actually has nothing to do will whatever the base is. It is the place holders or pattern that matters, can you see it now? – gravityboy Nov 25 '10 at 14:16

2 Answers2

1

Maybe this is gives a first start: Working Demo

It basically just takes a number and converts it to a base 7 number:

var map = 'abcdefg';

var n = 13
var out = '';
if(n == 0) out = map.charAt(0) + out;
while (n > 0) {
    out = map.charAt(n % 7) + out;
    n = Math.floor(n / 7);    
}   
// gives "bg" for 13

This would imply maintaining a normal counter in decimal system and convert every number.

Note that aa in this system does not exit, as a maps to 0.

Felix Kling
  • 795,719
  • 175
  • 1,089
  • 1,143
1
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.

Fred
  • 4,195
  • 2
  • 29
  • 42