-2

If you have any in a list item like 0123 then it will show abcd. again if it 5462 then will show fegc. my html code is below:

<div class="myList">
 - 0123
 - 5462
 - 0542
</div>
Converted output will be
//////////
<div class="myList">
 - abcd
 - fegb
 - afec
</div>

Is it possible to create using javascript?

Bendy
  • 3,506
  • 6
  • 40
  • 71
  • Have a look at this http://stackoverflow.com/questions/3145030/convert-integer-into-its-character-equivalent-in-javascript – Vibhesh Kaul Aug 11 '15 at 07:13
  • Yes, it is. Give it a try, and if you have a *specific* question along the way, post that question. Please take the [tour], have a look around, and read through the [help], in particular [*How do I ask a good question?*](/help/how-to-ask) – T.J. Crowder Aug 11 '15 at 07:15

3 Answers3

1

Try something like this

var str = '0123';
var new_str = '';
for (var i = 0, len = str.length; i < len; i++) {
  new_str += String.fromCharCode(97 + parseInt(str[i]));
}
alert(new_str)

The following will take the text from the element and change it accordingly while skipping non integer characters

$('.myList').text(convertNumbersToLetters($('.myList').text()))

function convertNumbersToLetters(numbers) {
  new_str = '';
  for (var i = 0; i < numbers.length; i++) {
    new_str += isInt(numbers[i]) ? String.fromCharCode(97 + parseInt(numbers[i])) : numbers[i];
  }
  return new_str;
}

function isInt(n) {
  return !isNaN(parseInt(n));
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="myList">- 0123 - 5462 - 0542</div>
DGS
  • 6,015
  • 1
  • 21
  • 37
  • Great @DGS !!! but, this is working with only one item ... but if i have a list and there is different number in every list item then how will i manage this ??? can you explain me please? – Shariar Hossain Aug 11 '15 at 07:30
0

Do it like this (@DGS edited)

var str = ['0123', '456'];
var new_str = '';

for(var j=0; j < str.length; j++)
{
for (var i = 0; i < str[j].length; i++) {
  new_str += String.fromCharCode(97 + parseInt(str[j][i]));
}
alert(new_str);
new_str = '';
}
Hopfeeyy
  • 7
  • 1
  • 6
0

This is specific to your question.

alpha = ['a','b','c','d','e','f','g','h','i','j'];
function toNewString(input){
  inputArray = input.split('');
  for(i=0;i<inputArray.length; i++){
      if(!isNaN(inputArray[i]))
      inputArray[i] = alpha[inputArray[i]];
  }   
  return inputArray.join('');
}
$('.myList').html(toNewString($('.myList').html()));
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.0/jquery.min.js"></script>
<div class="myList">
 - 0123
 - 5462
 - 0542
</div>
rrk
  • 15,677
  • 4
  • 29
  • 45