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!
Feivi99Asked:
Who is Participating?
 
ProculopsisCommented:

Try this instead

<!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 () {

$("#find").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] ) );
    }
  }
  var pattern = new RegExp( words, "gi" );
  $("#result").val( $("#sentence").val().match( pattern ) );
})

});

</script>

</head>
<body>

<input id="sentence" value="An apple a day keeps the doctor away" size="48"/> 
<input id="encoded" value="1-16-16-12-5, 16-5-1-18, 15-18-1-14-7-5" size="48"/> 
<input id="find" type="button" value="Find" /> 
<input id="result" readonly="true" size="48" /> 

</body>
</html>

Open in new window

0
 
Jini JoseSenior .Net DeveloperCommented:
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
0
 
Feivi99Author Commented:
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...
0
Ultimate Tool Kit for Technology Solution Provider

Broken down into practical pointers and step-by-step instructions, the IT Service Excellence Tool Kit delivers expert advice for technology solution providers. Get your free copy now.

 
CKY092Commented:
Here's some code for you to use. Obviously you could implement the functions as you see fit.

http://jsfiddle.net/boisebrewer/ph53b/
0
 
ProculopsisCommented:

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

0
 
Feivi99Author Commented:
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."
0
 
Feivi99Author Commented:
Proculopsis,

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

Thanks
0
 
ProculopsisCommented:

Bump
0
 
Feivi99Author Commented:
Perfect :-)

Thanks!
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.