Question

Convert number to 5 letters and repeat as number increase in php or js

As loop thru number++ y need a letter equivalent like:


1 = C
2 = D
3 = E
4 = F
5 = G

6 = C
7 = D
8 = E
9 = F
10 = G

11 = C
12 = D
13 = E
14 = F
15 = G

16 = C
Etc

Any idea on how to do it?

Many thanks un advance

No correct solution

OTHER TIPS

Use a string or array with the 5 letters, and use modulus to get the index you should use.

var letters = new Array('C', 'D', 'E', 'F', 'G');​​​​​

for (var i = 0; i < 25; i++) {
    $("#result").append(letters[i%letters.length]);
}
​

Demo

You also need to know the value of 'C', so you know the starting number, and the value of 'G' so you know the ending number.

Try

 $arr = array("C", "D", "E", "F", "G");
 $MAX = 100; //Maximum iterations

 for ($i=0; $i++; $i<$MAX) {
    echo $i+1 + " = ";
    echo $arr[ $i % 5 ];
    echo "\n";
 }

Javascript:

function GetLetter(num)
{
    var letters = ["C","D","E","F","G"];
    return letters[(num - 1) % 5];
}

Using builtin fromCharCode (ASCII code of 'A' = 65):

for (var i = 1; i <= 16; i++) {
    console.log(i, String.fromCharCode(65 + 2 + (i - 1) % 5));
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top