Link to home
Start Free TrialLog in
Avatar of Feivi99
Feivi99

asked on

javascript replace letters with numbers

Hello,

I'm trying to filter an input value against a list of words, but I don't want that list of words to be easily seen by someone perusing my site. I therefore want to first want to define each letter as a number, and then have an array with my "numberwords". So, for example, I need to define that a=1, b=2, etc etc. Then I have a list of numbers, which are actually fruit; i.e. an array which looks like this:"1-16-16-12-5, 2-5-1-18, 15-18-1-14-7-5" which is actually the words "apple, pear orange". A user will then enter a word or sentence in an input, and I need to check whether the word or a word in the sentence equals any of the fruits mentioned above.

Any help greatly appreciated!
Avatar of Jini Jose
Jini Jose
Flag of India image

first store all the required fruit names in letter format in to an array.
then convert the input word to number format.
then compare it
Avatar of Feivi99
Feivi99

ASKER

Thanks for the pointers, put if you could give me some code as to how to do all that, that would be great! I'm not so good with arrays etc...
SOLUTION
Avatar of CKY092
CKY092
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial

Try something like this:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html>
<head>
<title>http://www.experts-exchange.com/Programming/Languages/Scripting/JavaScript/Q_27422674.html</title>

<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.4.4/jquery.min.js"></script>
<script>

jQuery(document).ready( function () {

$("#decode").click( function() {
  var list = $("#encoded").val().split( "," );
  var words = "";
  var i = -1;
  while ( ++i != list.length ) {
    words += (i == 0)? "" : ",";
    var word = list[i].split( "-" );
    var j = -1;
    while ( ++j != word.length ) {
      words += String.fromCharCode( 64 + Number( word[j] ) );
    }
  }
  $("#decoded").val( words );
})

});

</script>

</head>
<body>

<input id="encoded" value="1-16-16-12-5, 16-5-1-18, 15-18-1-14-7-5" size="48"/> 
<input id="decode" type="button" value="Decode" /> 
<input id="decoded" readonly="true" size="48" /> 

</body>
</html>

Open in new window

Avatar of Feivi99

ASKER

CKY092,

Thanks for putting that code together. The only thing is, I want the array of words already to be in number format. See in the question above

 " Then I have a list of numbers, which are actually fruit; i.e. an array which looks like this:"1-16-16-12-5, 2-5-1-18, 15-18-1-14-7-5" which is actually the words "apple, pear orange". A user will then enter a word or sentence in an input, and I need to check whether the word or a word in the sentence equals any of the fruits mentioned above."
Avatar of Feivi99

ASKER

Proculopsis,

Can you explain where I would put the array to be checked against?

Thanks
ASKER CERTIFIED SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of Feivi99

ASKER

Perfect :-)

Thanks!