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
Asked
Active
Viewed 214 times
-2

Mark Cidade
- 98,437
- 31
- 224
- 236

VPDD
- 131
- 4
- 7
-
4[What have you tried?](http://www.whathaveyoutried.com/) – John Conde Jun 01 '12 at 16:41
6 Answers
2
Use a string or array with the 5 letters, and use modulus to get the index you should use.

Ignacio Vazquez-Abrams
- 776,304
- 153
- 1,341
- 1,358
0
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.

nycynik
- 7,371
- 8
- 62
- 87
0
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";
}

StuckAtWork
- 1,613
- 7
- 23
- 37
0
Javascript:
function GetLetter(num)
{
var letters = ["C","D","E","F","G"];
return letters[(num - 1) % 5];
}

Kendall Frey
- 43,130
- 20
- 110
- 148
0
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));
}