Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 279
  • Last Modified:

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!
0
Feivi99
Asked:
Feivi99
2 Solutions
 
Jini Jose.Net Team LeadCommented:
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
 
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
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
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:

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
 
ProculopsisCommented:

Bump
0
 
Feivi99Author Commented:
Perfect :-)

Thanks!
0

Featured Post

Upgrade your Question Security!

Your question, your audience. Choose who sees your identity—and your question—with question security.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now